#include <stdio.h>
struct Name {char d[11]};
int main (){
char str[11];
scanf("%s",str);
struct Name new = {str};
}
我想初始化 Name 结构体,但是有一个警告:suggest braces around initialization of subobject。
如何将读取的字符数组放入我的 Name 结构中?
最佳答案
有两种方式:
int main ()
{
char str[11];
scanf("%10s",str); // Make sure you don't read more than
// what the array can hold.
struct Name name1 = {"name"}; // This works only if you use string literal.
struct Name name2;
strcpy(name2.d, str); // Use this when you want to copy from another variable.
}
关于c - 如何初始化包含数组的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28843360/