如何给C中的每个整数赋予地址? [英] How address is given to each of the integer in C?

查看:175
本文介绍了如何给C中的每个整数赋予地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()





{



int x;

int * y;

x = 5;

y =& x;

printf(%d ,y);



printf(\ n%d,y + 1);



此程序的输出即将到来



22124

22128

这里我将y增加1;它会增加4 ....为什么?



我尝试过的事情:



为什么它是22128而不是22125?

main()


{

int x;
int *y;
x=5;
y=&x;
printf("%d",y);

printf("\n%d",y+1);

output of this program is coming

22124
22128
here I'm increasing y by 1; and it gets increase by 4....why?

What I have tried:

why it is 22128 instead of 22125?

推荐答案

Quote:



22124

22128

这里我将y增加1;它会增加4 ....为什么?


22124
22128
here I'm increasing y by 1; and it gets increase by 4....why?



这与int的大小有关。

当你向y添加1时,你不要在地址中添加1,在整数上添加大小为4.

C数据类型 [ ^ ]


y 是一个指向整数的指针。使用指针算术,使用项目的大小,这是 int 的四个字节。



想想指向数组的指针。要访问下一个项目,地址必须按项目大小递增:

y is a pointer to an integer. With pointer arithmetics, the size of the items is used which is four bytes for an int.

Just think of a pointer to an array. To access the next item, the address must be incremented by the size of the item:
int firstItem = y[0];
int *firstAddr = &y[0]; // or: y
int secondItem = y[1];
int *secondAddr = &y[1]; // or: y + 1


这篇关于如何给C中的每个整数赋予地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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