简体   繁体   English

函数中2-D char数组的长度

[英]length of a 2-D char array in a function

This is a small code snippet. 这是一个小代码片段。 In the function g(), is there any way to know the number of strings in a[] ie, in this case is 4. 在函数g()中,有没有办法知道[]中的字符串数,即在这种情况下为4。

In main I can get this with the help of sizeof() function, but since I am passing address to the function g() and my pc is of 64 bit that is sizeof(a) inside g() is 8 which the size of pointer, but I have to know the number of strings. 在main中我可以在sizeof()函数的帮助下得到这个,但是因为我将地址传递给函数g()而我的pc是64位,即sizeof(a)里面的g()是8的大小指针,但我必须知道字符串的数量。

void g(char *a[])
{
    printf("%d\n",sizeof(a)/sizeof(a[0]));
}
int main()
{
    char *a[]={"1","2","3","4"};
    printf("%d\n",sizeof(a)/sizeof(a[0]));
    g(a);
}

There are many very similar questions on SO already, that explain what arrays are, how they are passed to functions, and what the implications are. SO上已经有许多非常相似的问题,它们解释了什么是数组,它们如何传递给函数,以及它们的含义是什么。 This one, for example should provide you with enough information to work this one out... still: 例如,这个应该为您提供足够的信息来解决这个问题...仍然:

You can't do that, not reliably anyway. 不管怎么说,你不能这样做。 Once an array is passed to a function, it decays into a pointer (the size of which is 8 on a 64bit platform, 4 on 32 bit). 一旦数组传递给一个函数,它就会衰减成一个指针(64位平台的大小为8,4位为4)。 Your function argument char *a[] is what's known as an incomplete type. 你的函数参数char *a[]就是所谓的不完整类型。 It specifies an array of pointer, but that array is of an unspecified length. 它指定一个指针数组,但该数组的长度未指定。 What you could do is complete the type: 可以做的是完成类型:

void g(char *a[4])
{}

In which case, your code will work, but that's a tad dangerous, because the function might be called with an array of a different length. 在这种情况下,您的代码将起作用,但这有点危险,因为可能使用不同长度的数组调用该函数。

What is more common is to require a second argument to be passed (generally of size_t ) which specifies the length of the array that is passed: 更常见的是要求传递第二个参数(通常为size_t ),该参数指定传递的数组的长度:

void g(char *a[], size_t arr_len)
{}

This function is to be called like so: 这个函数被调用如下:

g(an_array, sizeof an_array/ sizeof *an_array);

Where an_array is, of course, an array, and sizeof an_array is its size in bytes, divided by the size of the type of the values within the array. 当然, an_array是一个数组,而sizeof an_array的大小是以字节为单位的大小,除以数组中值的类型大小。 sizeof *an_array is an alternative way of writing sizeof an_array[0] . sizeof *an_array是另一种编写sizeof an_array[0] It also relies on the fact that arrays decay into pointers most of the time, and since the C standard specifies array[0] as being the same as *(array + 0) , they are interchangeable... 它还依赖于数组在大多数时间内衰减为指针的事实,并且由于C标准将array[0]指定为与*(array + 0) ,因此它们是可互换的......

Just a nit-pick: you might want to change this: 只是挑剔:你可能想改变这个:

char *a[]={"1","2","3","4"};

To what you're actually declaring and initializing here: 你在这里实际宣布和初始化的内容:

const char *a[]={"1","2","3","4"};

The char * in a are constant, they cannot be altered as they reside in read-only memory, it'd be better to either allocate the memory yourself, or write char *a是不变的,它们不能被他们居住在只读存储器改变,它会是更好的用户可以自己分配内存,或写

char a[][] = {"1", "2", "3", "4"};

Which copies the string into RW memory 将字符串复制到RW存储器中

在C中,您需要将数组的大小传递给函数或使用sentinel值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM