C语言 结构体的指针运算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7623704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pointer arithmetic for structs
提问by user950891
Given a struct definition that contains one double and three int variables (4 variables in all), if p is a pointer to this struct with a value 0x1000, what value does p++ have?
给定一个包含一个 double 变量和三个 int 变量(总共 4 个变量)的结构体定义,如果 p 是指向该结构体的指针,值为 0x1000,那么 p++ 具有什么值?
This is not a homework problem, so don't worry. I'm just trying to prepare for a test and I can't figure out this practice problem. Thanks
这不是家庭作业的问题,所以不用担心。我只是想准备考试,我无法弄清楚这个练习题。谢谢
This is in C. Yes I want the value of p after it is incremented. This is a 32-bit machine
这是在 C 中。是的,我想要 p 递增后的值。这是一台 32 位机器
回答by tangrs
struct foobar *p;
p = 0x1000;
p++;
is the same as
是相同的
struct foobar *p;
p = 0x1000 + sizeof(struct foobar);
回答by Charlie Martin
The answer is that it is at least
答案是至少
sizeof(double) + (3*sizeof(int))
Thew reason it's "at least" is that the compiler is more or less free to add padding as needed by the underlying architecture to make it suit alignment constraints.
它“至少”的原因是编译器或多或少可以根据底层架构的需要自由添加填充以使其适合对齐约束。
Let's say, for example, that you have a machine with a 64-bit word, like an old CDC machine. (Hell, some of them had 60-bit words, so it would get weirder yet.) Further assume that on that machine, sizeof(double)is 64 bits, while sizeof(int)is 16 bits. The compiler might then lay out your struct as
例如,假设您有一台带有 64 位字的机器,就像一台旧的 CDC 机器。(见鬼,其中一些有 60 位字,所以它会变得更奇怪。)进一步假设在那台机器上,sizeof(double)是 64 位,而sizeof(int)16 位。然后编译器可能会将您的结构布置为
| double | int | int | int | 16 bits padding |
so that the whole struct could be passed through the machine in 2 memory references, with no shifting or messing about needed. In that case, sizeof(yourstruct_s) would be 16, where
sizeof(double)+ (3*sizeof(int))is only 4814.
这样整个结构就可以在 2 个内存引用中通过机器,不需要移动或混乱。在这种情况下,sizeof(yourstruct_s) 将是 16,其中
sizeof(double)+ (3*sizeof(int))只有4814。
Update
更新
Observe this could be true on a 32-bit machine as well: then you might need padding to fit it into threewords. I don't know of a modern machine that doesn't address down to the byte, so it may be hard to find an example now, but a bunch of older architectures ould need this.
注意可能是真实的32位计算机上,以及:那么你可能需要填充到它适合3个字。我不知道现代机器不会寻址到字节,所以现在可能很难找到一个例子,但是一堆旧的架构需要这个。
回答by James
p = p + sizeof(YourStruct)
The compiler is free to decide what sizeof will return if you don't turn padding off.
如果您不关闭填充,编译器可以自由决定返回什么 sizeof。
回答by nos
Pointer arithmetic is done in units of the size of the pointer type.
指针运算以指针类型的大小为单位进行。
So if you do p++on a pointer to your struct, p will advance by sizeof *pbytes. i.e. just ask your compiler for how big your struct is with the sizeofoperator.
因此,如果您p++在指向结构的指针上执行操作,则 p 将按sizeof *p字节前进。即只需询问您的编译器您的结构与sizeof运算符的大小。
回答by Saurabh Yadav
A Increment in base address of a data type is equal to base address + sizeof(data type)
数据类型基地址的增量等于基地址 + sizeof(data type)

