关于结构体地址问题

xidianxiancai 2009-01-07 04:30:16
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
struct node1{
char str[15]; /*必须要换成*str*/
int i;
};
void main()
{
char *string;
struct node1 *p;
printf("%o\n",string);
printf("%o\n",p->str);
printf("%o\n",p->i);
p=(struct node1 *)malloc(sizeof(struct node1));
if(p==NULL)
{printf("Can't get momery.\n");exit(1);}
scanf("%s",string);
printf("%o\n",string);
printf("%o\n",p->str);
printf("%o\n",p->i);
getch();
}
在TC环境下的运行结果:
6262
3173
40524
asdf(随便输的)
6262
6410
177520
这里有个疑问:为什么string前后的地址是一样的?而p->str和p->i的地址前后不一致?
还有str和i在一个结构体里面,它们的地址为什么隔这么远,而不是挨着的呢?不理解。
问题问题有点傻。请指教。谢谢!
...全文
79 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lipenghui77 2009-01-07
  • 打赏
  • 举报
回复
printf("%o\n",&(p->i));
malloc之前,没有初始化,应该是随机值。malloc之后,p指向堆区。
davelv 2009-01-07
  • 打赏
  • 举报
回复
你输出的貌似不是地址....使用%x 并&操作符
submarine2007 2009-01-07
  • 打赏
  • 举报
回复
printf("%o\n",string); // string 没有初始化, 如果你想打印地址 &string
printf("%o\n",p->str); // 这里 str == &str
printf("%o\n",p->i); // 同string, &p->i
xyllucky 2009-01-07
  • 打赏
  • 举报
回复
抢分呀

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧