保证数组的元素从低存储到更高的地址? [英] Are the elements of an array guaranteed to be stored from lower to higher addresses?

查看:345
本文介绍了保证数组的元素从低存储到更高的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数组:

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

时它总是保证的&放大器;列表[0]<&安培;列表[1];&放大器;名单[2]

我曾以为它是同时使用C一个硬性规定,但现在必须非常肯定它作为一个OP刚才问我这件事时,我回答了他关于字节序

I had assumed it to be a hard and fast rule while using C, but now have to very sure about it as an OP just asked me about it when I answered his question about endianness

<一个href=\"http://stackoverflow.com/questions/16395907/little-endian-or-big-endian/16396005#16396005\">Little端或大端

什么给了我第二个想法是堆栈可以成长向上或向下 issue.I我不是很肯定,这么严格的答案是AP preciated.Thanks

What gave me second thoughts is the stacks can grow up or down issue.I am not very sure about that so your rigorous answers are appreciated.Thanks.

推荐答案

是的,它保证了&放大器;列表[0]&LT;&安培;列表[1] &放大器;列表[1];&安培;名单[2] 。当指针指向同一阵列的元素进行比较,指针具有较大标元素会被认为具有更大的值。这是在C99 6.5.8@5规定:

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

但是,它不能保证通过的printf印有%P 的值也将遵循同样的顺序 - 这些值是实现定义

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

这篇关于保证数组的元素从低存储到更高的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆