我正在尝试在main()中初始化struct类型的数组,但是编译器返回错误field designator cannot initialize a non-struct, non-union type
我的代码:
struct details{
const char *author;
const char *title;
};
int main()
{
const char *input = "Data Structures and Algorithm";
struct details a [] = {
.author = "Narsimha",
.title = input
};
printf("%s\n", a[0].author);
printf("%s\n", a[0].title);
return 0;
}
gcc inputs.c
inputs.c:16:9: error: field designator cannot initialize a non-struct, non-union type 'struct details []'
.author = "Narsimha",
^
inputs.c:17:9: error: field designator cannot initialize a non-struct, non-union type 'struct details []'
.title = input
^
2 errors generated.
答案 0 :(得分:4)
您缺少一对牙套。试试这个:
struct details a [] = {{
.author = "Narsimha",
.title = input
}};
外部括号用于定义数组。内括号用于struct
。