struct student stu[N].id = {"A01","A02","A03","A04","A05","A06"};这句话有错吗
时间: 2023-10-16 07:12:07 浏览: 48
这句话有语法错误。应该将 ".id" 改为 ".name",因为结构体中没有名为 "id" 的成员变量,而是有名为 "name" 的成员变量。正确的写法应该是:
struct student stu[N] = {{"A01"},{"A02"},{"A03"},{"A04"},{"A05"},{"A06"}};
其中,每个花括号内的字符串表示一个学生的姓名,对应结构体中的 "name" 成员变量。
相关问题
int main(){ struct{ char *name; int num; int age; char group; char float score; }stu1; stu1.name = "Tom"; stu1.num =12; stu1.age =18; stu1.group = 'A'; stu1.score = 136.5; printf("%s的学号是%d”,stu1.name ,stu.age); return 0;这段代码有什么问题吗
这段代码有几个问题:
1. 在结构体定义中,变量 score 应该是 float 类型,而不是 char 类型。
2. 在 printf 语句中,变量 stu 应该是 stu1。
修改后的代码应该是这样的:
```
int main(){
struct {
char *name;
int num;
int age;
char group;
float score;
} stu1;
stu1.name = "Tom";
stu1.num = 12;
stu1.age = 18;
stu1.group = 'A';
stu1.score = 136.5;
printf("%s的学号是%d", stu1.name, stu1.num);
return 0;
}
```
int main(){ struct { char *name; int num; int age; char group; float score; } stu1; stu1.name = "Tom"; stu1.num = 12; stu1.age = 18; stu1.group = 'A'; stu1.score = 136.5; printf("%s的学号是%d", stu1.name, stu1.num); return 0; }这段代码的运行结果是什么
这段代码的运行结果是:
```
Tom的学号是12
```
代码定义了一个结构体 `stu1`,包含五个成员:`name`、`num`、`age`、`group` 和 `score`。然后给这个结构体的各个成员赋值,并打印出其中的 `name` 和 `num` 成员,输出结果为 `Tom的学号是12`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)