[英]How to Initialise array of type struct with DOT operator
我正在嘗試初始化main()中的struct類型的數組,但是編譯器返回錯誤field designator cannot initialize a non-struct, non-union type
My Code:
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.
您缺少一對牙套。 試試這個:
struct details a [] = {{
.author = "Narsimha",
.title = input
}};
外部括號用於定義數組。 內部花括號用於該struct
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.