在写结构体的时候,我发现了这样的一个现象
#include<stdio.h>
int prt(struct student *stu);
struct student
{
char *name;
float score[3];
};
int main()
{
struct student stu;
stu.name = "cxk";
stu.score[0] = 98.5;
stu.score[1] = 97.5;
stu.score[2] = 99.5;
prt( &stu );
}
int prt(struct student *stu)
{
printf("name : %s\n",stu -> name);
printf("socre[0] : %f\n",stu -> score[0]);
printf("socre[1] : %f\n",stu -> score[1]);
printf("socre[2] : %f\n",stu -> score[2]);
}
这样写会有报错
指针不兼容,冲突。
我在网上搜了半天没搞懂这是为什么
但如果将函数声明放在main函数中
#include<stdio.h>
struct student
{
char *name;
float score[3];
};
int main()
{
int prt(struct student *stu);
struct student stu;
stu.name = "cxk";
stu.score[0] = 98.5;
stu.score[1] = 97.5;
stu.score[2] = 99.5;
prt( &stu );
}
int prt(struct student *stu)
{
printf("name : %s\n",stu -> name);
printf("socre[0] : %f\n",stu -> score[0]);
printf("socre[1] : %f\n",stu -> score[1]);
printf("socre[2] : %f\n",stu -> score[2]);
}
就没有错误,希望有大神可以指点一下