[英]Issue initializing array inside struct
I'm trying to get a structure, which contains an integer and a pointer to another structure.我正在尝试获取一个结构,其中包含一个整数和一个指向另一个结构的指针。 This second structure is just an array of 2 strings and an array of 2 numbers.第二个结构只是一个由 2 个字符串组成的数组和一个由 2 个数字组成的数组。
What am I doing wrong here?我在这里做错了什么?
struct folder {
char **filenames_array;
int *images_array;
};
struct display {
int pos_y;
struct folder current_folder;
};
struct display g_display = {
.pos_y = 0,
.current_folder = {
.filenames_array = {"002.jpg", "003.jpg"},
.images_array = {0, 0},
}
};
I'm getting those errors:我收到这些错误:
error C2143: syntax error : missing '}' before '.'
error C2143: syntax error : missing ';' before '.'
error C2059: syntax error : '.'
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)
error C2059: syntax error : '}'
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : '}'
You can do this with compound literal syntax:您可以使用复合文字语法来做到这一点:
struct display g_display = {
.pos_y = 0,
.current_folder = {
.filenames_array = (char *[]){"002.jpg", "003.jpg"},
.images_array = (int []){0, 0},
}
};
Alternately, if you know your arrays will always have two elements, you can keep the current initializer syntax and declare the arrays as actual arrays instead of pointers:或者,如果你知道你的数组总是有两个元素,你可以保留当前的初始化语法并将数组声明为实际数组而不是指针:
struct folder {
char *filenames_array[2];
int images_array[2];
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.