#include<stdio.h>
struct student
{
char name[50];
int age;
int score;
};
int main()
{
struct student stu={"zhangsan",20,90};
strcpy(stu.name,"lisi");
stu.age=18;
stu.score=100;
printf("name=%s,age=%d,score=%d",stu.name,stu.age,stu.score);
return 0;
}
运行不了是怎么个现象呢???
需要#include <string.h>吧
头文件打错了:#include <stdio.h> //#include <studio.h> 多了'u'
缺了头文件: #include <string.h> // strcpy() 函数的头文件
#include<stdio.h>
struct student
{
char *name;
int age;
int score;
};
int main()
{
struct student stu;
stu.name=&"lisi";
stu.age=18;
stu.score=100;
printf("name=%s,age=%d,score=%d",stu.name,stu.age,stu.score);
return 0;
}
调用strcpy函数的话,需要用头文件#include<string.h>
换一个编译器,运行
#include <stdio.h>
struct student
{
char name[50]; //学生姓名
int age; //学生年龄
int score; //学生分数;
}; // 结构体声明时末尾一定要加";"
int main()
{
//定义并且初始化结构体数组
struct student a[5] = {
{"zhangsan", 23, 80},
{"ls", 18, 90},
{"laowang", 25, 100},
{"zhaolu", 29, 50},
{"maqi", 30, 65}
};
int i;
int sum = 0;
int avarage; //存储平均分
for (i = 0; i < 5; i++)
{
sum = sum + a[i].score; //计算总成绩
printf("sum=%d\n",sum);
}
return 0;
}