|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前面这两种结构体类型指针定义是一样的吗,都是定义他们为首地址,如果不一样第二种定义是什么意思?如果一样为什么我最下面那段代码老是报错呢,求指教。
- struct student
- {
- int num;
- float score;
- struct student *next;
- }stu,p1,p2;
- struct student *stu,*p1,*p2;
复制代码
- struct student
- {
- int num;
- float score;
- struct student *next;
- }*stu,*p1,*p2;
复制代码
- #include <stdio.h>
- #if(1)
- struct object
- {
- float Chinese;
- float Math;
- float English;
- }obj;
- #endif
- struct student
- {
- int num;
- char *name;
- struct object obj;
- }*stu;
- int main(void)
- {
- void print(struct student *p);
- struct student *stu;
- *stu={8,"JJ Lin",{98.5,90.0,95.5}};
- print(stu);
- }
- void print(struct student *p)
- {
- printf("num :%d\n",(*p).num);
- printf("name :%s\n",(*p).name);
- printf("Chinese score :%.1f\n",(*p).obj.Chinese);
- printf("Math score :%.1f\n",p->obj.Math);
- printf("English score :%.1f\n",p->obj.English);
- }
复制代码
本帖最后由 jackz007 于 2021-1-19 18:11 编辑
- struct student
- {
- int num;
- float score;
- struct student * next ;
- }stu , p1 , p2 ;
- struct student * stu , * p1 , * p2 ; // 错误,相同的变量被重复定义
复制代码
- struct student
- {
- int num;
- float score;
- struct student * next ;
- } * stu ,* p1 ,* p2 ; // 所有变量定义正确有效
复制代码
- #include <stdio.h>
- #if(1)
- struct object
- {
- float Chinese ;
- float Math ;
- float English ;
- } obj ;
- #endif
- struct student
- {
- int num ;
- char name[24] ; // name 需要实质性保存字符串,所以,不可以定义成指针
- struct object obj ;
- } * stu ;
- int main(void)
- {
- void print(struct student * p) ;
- struct student stu = {8,"JJ Lin",{98.5,90.0,95.5}} ; // stu 需要保存数据,不可以定义成指针,结构整体赋值只能在定义时进行
- print(& stu) ;
- }
- void print(struct student *p)
- {
- printf("num :%d\n",(*p).num);
- printf("name :%s\n",(*p).name);
- printf("Chinese score :%.1f\n",(*p).obj.Chinese);
- printf("Math score :%.1f\n",p->obj.Math);
- printf("English score :%.1f\n",p->obj.English);
- }
复制代码
|
|