【发布时间】:2022-01-20 14:05:44
【问题描述】:
int main()
{
struct Student_struct {
char name[40];
int age;
float grade;
};
struct Student_struct student;
printf("---------------------Student-----------------------\n\n\n");
student.name[] = "person";
student.age = 20;
student.grade = 7.5;
return 0;
}
我收到以下错误:']'之前的预期表达式
我知道我可以使用 strcpy(student.name, "person") 或 student.name[6] = "person",但为什么不能将其编码为 student.name[] = "person"?这背后的逻辑是什么?
【问题讨论】:
-
请编辑您的帖子以添加代码格式和缩进。
-
student.name[] = "person"==>strcpy(student.name, "person");别忘了#include <string.h> -
student.name[6] = "person"不会做你想做的事,student.name[] = "person"只是语法不正确。 -
因为
name[]没有任何意义。name已经是一个数组了,[]添加了什么? -
还要注意
char name[6] = "person"是一个错误。您需要至少 7 个字符来保存字符串。
标签: arrays c variables struct char