本帖最后由 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);
}
|