在我的代码中:
char *str[] = {"forgs", "do", "not", "die"}; printf("%d %d", sizeof(str), sizeof(str[0]));我得到的输出为 12 2,所以我的怀疑是:
I'm getting the output as 12 2, so my doubts are:
在大多数情况下,数组名会衰减到它的第一个元素的地址的值,并且 type 与指向元素类型的指针相同.因此,您会期望一个裸 str 的值等于 &str[0],并且类型指针指向 char 的指针.
In most cases, an array name will decay to the value of the address of its first element, and with type being the same as a pointer to the element type. So, you would expect a bare str to have the value equal to &str[0] with type pointer to pointer to char.
然而,sizeof 并非如此.在这种情况下,数组名称保持其 sizeof 的类型,这将是指向 char 的 4 个指针的数组.
However, this is not the case for sizeof. In this case, the array name maintains its type for sizeof, which would be array of 4 pointer to char.
sizeof 的返回类型是 size_t.如果你有C99编译器,你可以在格式字符串中使用%zu打印sizeof返回的值.
The return type of sizeof is a size_t. If you have a C99 compiler, you can use %zu in the format string to print the value returned by sizeof.
更多推荐
在字符指针数组的情况下,“数组名称"是什么意思?
发布评论