- 论坛徽章:
- 0
|
本帖最后由 十年梦生 于 2013-03-02 02:35 编辑
程序如下:- #include <stdio.h>
- struct test{
- int a;
- double b;
- int c;
- };
- void main(void){
- struct test tmp;
- printf("%x-----%x\n",&tmp.a,&tmp.c);
-
- printf("%d\n",&tmp.c-&tmp.a);
- printf("%d\n",(unsigned int)&tmp.c - (unsigned int)&tmp.a);
- printf("size of int:%d,size of double:%d\n",sizeof(int),sizeof(double));
- }
复制代码 输出结果为:
44fcc8-----44fcd8
4
16
size of int:4,size of double:8
问题一个:
结构体成员指针相减,这个4是如何得出来的。
同时实验:
struct test{
char a;
double b;
char c;
};
&tmp.c-&tmp.a结果为16
struct test{
char a;
char b;
double c;
};
&tmp.c-&tmp.a结果为1
struct test{
char a;
char b;
char c[20];
};结果为0
求解释为什么
-----------------------------------------------------
自问自答吧。。。
https://www.securecoding.cert.or ... r+to+the+same+array
这个链接里解释了详细原因。
|
|