函数参数中数组的长度 [英] Length of array in function argument

查看:21
本文介绍了函数参数中数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在 C 中计算数组长度的众所周知的代码:

This is well known code to compute array length in C:

sizeof(array)/sizeof(type)

但我似乎无法找出作为参数传递给函数的数组的长度:

But I can't seem to find out the length of the array passed as an argument to a function:

#include <stdio.h>

int length(const char* array[]) {
  return sizeof(array)/sizeof(char*);
}

int main() {
  const char* friends[] = { "John", "Jack", "Jim" };
  printf("%d %d", sizeof(friends)/sizeof(char*), length(friends)); // 3 1
}

我假设数组是按值复制到函数参数作为常量指针和引用它应该解决这个问题,但这个声明是无效的:

I assume that array is copied by value to the function argument as constant pointer and reference to it should solve this, but this declaration is not valid:

int length(const char**& array);

我发现将数组长度作为第二个参数传递是多余的信息,但为什么main的标准声明是这样的:

I find passing the array length as second argument to be redundant information, but why is the standard declaration of main like this:

int main(int argc, char** argv);

请说明是否可以在函数参数中找出数组长度,如果可以,为什么main中有冗余.

Please explain if it is possible to find out the array length in function argument, and if so, why is there the redundancy in main.

推荐答案

sizeof 仅适用于在将其应用于原始数组时查找数组的长度.

sizeof only works to find the length of the array if you apply it to the original array.

int a[5]; //real array. NOT a pointer
sizeof(a); // :)

然而,当数组衰减为指针时,sizeof 将给出指针的大小,而不是数组的大小.

However, by the time the array decays into a pointer, sizeof will give the size of the pointer and not of the array.

int a[5];
int * p = a;
sizeof(p); // :(

正如您已经聪明地指出的,main 接收数组的长度作为参数 (argc).是的,这是必要的,不是多余的.(嗯,这有点多余,因为 argv 很方便地由空指针终止,但我离题了)

As you have already smartly pointed out main receives the length of the array as an argument (argc). Yes, this is out of necessity and is not redundant. (Well, it is kind of reduntant since argv is conveniently terminated by a null pointer but I digress)

关于为什么会发生这种情况有一些推理.我们怎样才能让 C 数组也知道它的长度?

There is some reasoning as to why this would take place. How could we make things so that a C array also knows its length?

第一个想法是在将数组传递给函数时不会衰减为指针,并继续在类型系统中保持数组长度.这样做的坏处是您需要为每个可能的数组长度设置一个单独的函数,而这样做并不是一个好主意.(Pascal 做到了这一点,有人认为这是它输给"C 的原因之一)

A first idea would be not having arrays decaying into pointers when they are passed to a function and continuing to keep the array length in the type system. The bad thing about this is that you would need to have a separate function for every possible array length and doing so is not a good idea. (Pascal did this and some people think this is one of the reasons it "lost" to C)

第二个想法是将数组长度存储在数组旁边,就像任何现代编程语言一样:

A second idea is storing the array length next to the array, just like any modern programming language does:

a -> [5];[0,0,0,0,0]

但是,您只是在幕后创建了一个不可见的struct,C 哲学不赞成这种开销.也就是说,对于某些类型的问题,自己创建这样的结构通常是一个好主意:

But then you are just creating an invisible struct behind the scenes and the C philosophy does not approve of this kind of overhead. That said, creating such a struct yourself is often a good idea for some sorts of problems:

struct {
    size_t length;
    int * elements;
}

<小时>

您可以考虑的另一件事是 C 中的字符串如何以空结尾而不是存储长度(如在 Pascal 中).要在不担心限制的情况下存储长度需要 高达 四个字节,这是一个难以想象的昂贵数量(至少在当时是这样).有人可能想知道数组是否也可以像这样以 null 结尾,但是您将如何允许数组存储 null?


Another thing you can think about is how strings in C are null terminated instead of storing a length (as in Pascal). To store a length without worrying about limits need a whopping four bytes, an unimaginably expensive amount (at least back then). One could wonder if arrays could be also null terminated like that but then how would you allow the array to store a null?

这篇关于函数参数中数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆