关于typedef 定义结构体指针赋值的问题

teng27 2011-12-01 01:55:25
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct per
{
char name[20];
char add[20];
int age;
}Person; 如果将下面给的 Person *s 合并到这里: Person,*s; 当执行到s=a的时候 编译就出错 为什么呢?

int main(void)
{
Person *s;
int i;
char *p;
Person a[3]={
{"강민","서울",10},
{"김강","울산",12},
{"박수","대구",9}
};
s=a;
printf("plesae input a string.\n");
scanf("%s",p);
for(i=0;i<3;i++,s++)
if(strcmp(s->name,p)==0)
{
printf("find the people\nthis people's informatation as fallow\n");
printf("%s\t%s\t%d",s->name,s->add,s->age);
}
else
{
printf("NO this people.\n");
exit(0);
}
用 vs 2010 编译可以通过 但是如果按照上面修改了以后就不行
程序可能不完善 还有错的地方 请大家帮忙改改
...全文
744 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
teng27 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cloudzay 的回复:]

引用 1 楼 cloudzay 的回复:

如果将下面给的 Person *s 合并到这里: Person,*s; 当执行到s=a的时候 编译就出错 为什么呢?

这就相当于给Person *类型命名为s了
因为你有typedef 所以整个相当于 typedef struct {} * s

也即s成为了一个类型名。。。
[/Quote]原来 如此 非常感谢
雲哉 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cloudzay 的回复:]

如果将下面给的 Person *s 合并到这里: Person,*s; 当执行到s=a的时候 编译就出错 为什么呢?

这就相当于给Person *类型命名为s了
因为你有typedef 所以整个相当于 typedef struct {} * s
[/Quote]
也即s成为了一个类型名。。。
雲哉 2011-12-01
  • 打赏
  • 举报
回复
如果将下面给的 Person *s 合并到这里: Person,*s; 当执行到s=a的时候 编译就出错 为什么呢?

这就相当于给Person *类型命名为s了
因为你有typedef 所以整个相当于 typedef struct {} * s

69,371

社区成员

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

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