Are the elements of an array guaranteed to be stored from lower to higher addresses?

后端 未结 3 1561
遥遥无期
遥遥无期 2021-02-09 23:35

Suppose I have the following array:

int list[3]={2,8,9};
printf(\"%p,%p,%p\",(void*)&list[0],(void*)&list[1],(void*)&list[2]);

Is i

3条回答
  •  终归单人心
    2021-02-10 00:15

    Yes, it's guaranteed that &list[0]<&list[1] and &list[1]<&list[2]. When pointers to elements of the same array are compared, the pointer to the element with the larger subscript will be considered to have larger value. This is specified in C99 6.5.8@5:

    pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values

    However, it is not guaranteed that the values printed by printf with %p will also follow the same ordering - these values are implementation-defined.

提交回复
热议问题