开发者

In C, is the address of the first element in an array the same as the address of the array?

Consider this:

int i[50];
void *a = i; //i.e. = &i[0]
void *b = &i;

Will a == b always be开发者_Go百科 true or are there platforms/compilers where this might not always be true ?


yes, paragraph 6.5.9 of the standard (equality operator) says:

Two pointers compare equal if ... both are pointers to the same object (including a pointer to an object and a subobject at its beginning)


Yes, the value cast to void* is the same ... but the original type isn't.

Having

int arr[100];

the value arr, when it decays to a pointer to its first element, has type *int;
the value &arr has type int (*)[100]


In this particular case, yes both a and b would be reported to be same, since the array in question is 1D. FYI - this does not hold true had it been a 2D or higher dimensional array in question? Read more about pointer to arrays,in that case. Good to know concept, though better avoided for maintaining good code readability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜