什么是“数组名”的字符指针数组的情况下,是什么意思? [英] What does the 'array name' mean in case of array of char pointers?
问题描述
在我的code:
的char * str中[] = {forgs,做,不是,死};
的printf(%D的sizeof(STR)的sizeof(海峡[0]));
我得到的输出为 12 2
,所以我的疑惑是:
- 为什么有区别吗?
- 双方
STR
和海峡[0]
是字符指针,对吧?
在大多数情况下,一个阵列名称将衰减到其第一元素的地址的值,以及与类型为相同的指针的元素类型。所以,你会期望一个光秃秃的 STR
有等于值&放大器;海峡[0]
型指针指针字符
。
然而,这不是sizeof的为的情况。在这种情况下,数组名一直为
类型的sizeof
,这将是4指针数组字符
。
的sizeof的
的返回类型是为size_t
。如果你有一个C99编译器,可以使用%祖
在格式字符串打印由的sizeof
返回的值。
In my code:
char *str[] = {"forgs", "do", "not", "die"};
printf("%d %d", sizeof(str), sizeof(str[0]));
I'm getting the output as 12 2
, so my doubts are:
- Why is there a difference?
- Both
str
andstr[0]
are char pointers, right?
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
.
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
.
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
.
这篇关于什么是“数组名”的字符指针数组的情况下,是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!