为什么这不起作用:
#define PORT_ID_MAX_CHAR 6
typedef struct {
int phys;
char name[PORT_ID_MAX_CHAR];
}tPortMap;
struct tPortMap c_portMap[] = { 0, "test" }, { 1,"test" };GCC对我咆哮着说myfile.c:8:46: error: expected identifier or ‘(’ before ‘{’ token struct tPortMap c_portMap[] = { 0, "test" }, { 1,"test" };,我不知道为什么.我很困惑..。
EDIT1
使用额外的大括号,我得到了错误:struct tPortMap c_portMap[] = {{ 0, "test" }, { 1,"test" }};
myfile.c:8:17: error: array type has incomplete element type struct tPortMap c_portMap[] = {{ 0, "test" }, { 1,"test" }};
发布于 2016-03-14 18:33:23
您需要另一对环绕数组元素数据的大括号。
而且,您不需要使用struct tPortMap,因为您已经使用了typedefed tPortMap。
tPortMap c_portMap[] = { { 0, "test" }, { 1,"test" } };
^^ ^^当你使用
struct tPortMap c_portMap[] = { { 0, "test" }, { 1,"test" } };编译器认为您正在声明一个新的struct,这显然是不完整的。
发布于 2016-03-14 18:44:26
试一试:
#include <stdio.h>
#define PORT_ID_MAX_CHAR 6
typedef struct tPortMap {
int phys;
char name[PORT_ID_MAX_CHAR];
}tPortMap;
int main(void)
{
tPortMap c_portMap[] = { { 0, "test" }, { 1,"test" } };
printf("%s\n", c_portMap[0].name);
return 0;
}https://stackoverflow.com/questions/35995239
复制相似问题