结构成员偏移。 [英] Structure member offset.
问题描述
#include< stdio.h>
#include< stdlib.h>
struct s
{
int i;
char c;
float f;
};
int main()
{
printf(" addr is [%p]。\ n",&(((struct s * )0) - > c));
返回EXIT_SUCCESS;
}
当我编译时(gcc -g - W -Wall -ansi -pedantic)并运行上面的代码,
打印addr is [0x4]。。如果我改为0到100,则打印104.
为什么?
标准的哪一部分要求这种行为?
谢谢,
--GS
你得到的答案是正确的因为首先在结构中使用
元素是整数类型,所以char c距你给的
基地址4个字节。
如果你在你的结构中交换float f和char c的位置,
char c的偏移量将是你给出的基地址的8。
-Raghu。
" gokrix" <去**** @ hotmail.com>在消息中写道
news:43 ******** @ news.beasys.com ...#include< stdio.h>
#include< stdlib.h>
struct s
{i /;
char c;
浮动f;
} ;
int main()
{/> printf(" addr is [%p]。\ n",&(((struct s *)0) - > c));
返回EXIT_SUCCESS;
}
当我编译(gcc -g -W -Wall -ansi -pedantic)并运行上面的代码时,
为什么?
标准的哪一部分要求这种行为?
谢谢,
--GS
" gokrix" <去**** @ hotmail.com>在消息中写道
news:43 ******** @ news.beasys.com ...#include< stdio.h>
#include< stdlib.h>
struct s
{i /;
char c;
浮动f;
} ;
int main()
{/> printf(" addr is [%p]。\ n",&(((struct s *)0) - > c));
返回EXIT_SUCCESS;
}
当我编译(gcc -g -W -Wall -ansi -pedantic)并运行上面的代码时,
为什么?
标准的哪一部分要求这种行为?
逻辑位;-)
>
您使用0或者100作为地址,然后将其转换为struct s
指针。然后你获取成员''''的地址 - 后面是
成员''我'。我是一个int,你的编译器似乎是32位。
32位= 4个字节。所以,地址0,1,2,3是我,因此''c''
从4开始。
据推测,你真的想要一个结构,例如,
struct sj;
printf(" addr is [%p]。\ n" ;,&(((struct s *)& j) - > c));
pemo写道:" gokrix" <去**** @ hotmail.com>在消息中写道
新闻:43 ******** @ news.beasys.com ...
#include< stdio.h>
#include< stdlib.h>
struct s
{i /;
char c;
浮动f;
} ;
int main()
{/> printf(" addr is [%p]。\ n",&(((struct s *)0) - > c));
返回EXIT_SUCCESS;
}
当我编译(gcc -g -W -Wall -ansi -pedantic)并运行上面的代码时,
为什么?
标准的哪一部分要求这种行为?
逻辑位;-)
你使用0 - 或 - 100作为地址,然后将其转换为struct s
指针。然后你取成员'c'的地址 - 后面是
成员''我'。我是一个int,你的编译器似乎是32位。
32位= 4个字节。因此,地址0,1,2,3是''我',因此''c''
从4开始。
据推测,你实际上想要一个结构,例如,
struct sj;
printf(" addr is [%p]。\ n",&(((struct s *)& j) - > c));
也许我可以更好地解决这个问题,方法是为什么
这不是一个未定义的操作吗?我的印象是
对一个不属于你的地址的'' - >''操作(比如说一个
随机地址,比如0或100)导致未定义的行为。
谢谢,
--GS
#include <stdio.h>
#include <stdlib.h>
struct s
{
int i;
char c;
float f;
};
int main()
{
printf("addr is [%p]. \n", &(((struct s*)0)->c));
return EXIT_SUCCESS;
}
When I compile (gcc -g -W -Wall -ansi -pedantic) and run the above code,
it prints "addr is [0x4].". If I change to 0 to 100, it prints 104.
Why?
Which part of the standard mandates this behaviour?
Thanks,
--GS
The answer you are getting is correct , becuase in the structure first
element is of integer type , so the "char c " lies 4 bytes away from the
base address you give.
if you interchange position of float f and char c in your structure , the
offset for char c will be 8 from the base address you give.
-Raghu.
"gokrix" <go****@hotmail.com> wrote in message
news:43********@news.beasys.com...#include <stdio.h>
#include <stdlib.h>
struct s
{
int i;
char c;
float f;
};
int main()
{
printf("addr is [%p]. \n", &(((struct s*)0)->c));
return EXIT_SUCCESS;
}
When I compile (gcc -g -W -Wall -ansi -pedantic) and run the above code,
it prints "addr is [0x4].". If I change to 0 to 100, it prints 104.
Why?
Which part of the standard mandates this behaviour?
Thanks,
--GS
"gokrix" <go****@hotmail.com> wrote in message
news:43********@news.beasys.com...#include <stdio.h>
#include <stdlib.h>
struct s
{
int i;
char c;
float f;
};
int main()
{
printf("addr is [%p]. \n", &(((struct s*)0)->c));
return EXIT_SUCCESS;
}
When I compile (gcc -g -W -Wall -ansi -pedantic) and run the above code,
it prints "addr is [0x4].". If I change to 0 to 100, it prints 104.
Why?
Which part of the standard mandates this behaviour?
The logic bit ;-)
You''re using 0 -or- 100 as an address, and then casting that to struct s
pointer. You then take the address of the member ''c'' - which comes after
member ''i''. i is an int, and appears to be 32-bits with your compiler.
32-bits = 4 bytes. So, addresses 0, 1, 2, 3 are ''i'', and therefore ''c''
starts at 4.
Presumably, you actually want a struct s, e.g.,
struct s j;
printf("addr is [%p]. \n", &(((struct s*)&j)->c));
pemo wrote:"gokrix" <go****@hotmail.com> wrote in message
news:43********@news.beasys.com...#include <stdio.h>
#include <stdlib.h>
struct s
{
int i;
char c;
float f;
};
int main()
{
printf("addr is [%p]. \n", &(((struct s*)0)->c));
return EXIT_SUCCESS;
}
When I compile (gcc -g -W -Wall -ansi -pedantic) and run the above code,
it prints "addr is [0x4].". If I change to 0 to 100, it prints 104.
Why?
Which part of the standard mandates this behaviour?
The logic bit ;-)
You''re using 0 -or- 100 as an address, and then casting that to struct s
pointer. You then take the address of the member ''c'' - which comes after
member ''i''. i is an int, and appears to be 32-bits with your compiler.
32-bits = 4 bytes. So, addresses 0, 1, 2, 3 are ''i'', and therefore ''c''
starts at 4.
Presumably, you actually want a struct s, e.g.,
struct s j;
printf("addr is [%p]. \n", &(((struct s*)&j)->c));
Maybe I could have framed the question better, along the lines of "Why
is this not an undefined operation?". I was under the impression that
the ''->'' operation on an address that does not belong to you (say a
random address like 0 or 100) leads to undefined behaviour.
Thanks,
--GS
这篇关于结构成员偏移。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!