C 编译器无意义的错误?

标签 c struct compiler-errors strcpy

我有以下用 C 编写的代码:

n.    struct UDSData {
        char *name;
        char *address;
      };

n.    char UDS1[16] = "fill up sixteen", UDS2[16] = "fill up sixteen";

n.    while (something) {
       ...

108.   char UDS1Temp[16], UDS2Temp[16];
109.   strcpy(UDS1Temp, UDS1);
110.   strcpy(UDS2Temp, UDS2);
111.   
112.   struct UDSData item = {UDS1Temp, UDS2Temp};
113.   UDSCodes[UDSTotal++] = item;
     }

知道为什么代码编译会出现这些错误:

1><file>(112): error C2143: syntax error : missing ';' before 'type'
1><file>(113): error C2065: 'item' : undeclared identifier
1><file(113): error C2440: '=' : cannot convert from 'int' to 'UDSData'

删除 strcpy() 并将 UDS1UDS2 直接输入到结构中。

最佳答案

您几乎可以肯定使用的是早期编译器标准,例如 C89,它不允许混合声明和代码。您需要在代码块的开头附近声明 item。像这样:

char UDS1Temp[16], UDS2Temp[16];
struct UDSData item = {UDS1Temp, UDS2Temp};

strcpy(UDS1Temp, UDS1);
strcpy(UDS2Temp, UDS2);
UDSCodes[UDSTotal++] = item

由于您只是将指针放入结构中,因此可以在 strcpy 之前完成初始化。但是您必须在这两个字符数组之后声明UDSData

关于C 编译器无意义的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15921180/

相关文章:

c - CLOCK_REALTIME CLOCK_MONOTONIC 中哪个时钟最适合计算开始时间结束时间

python - 在 Python 中通过 pyarrow 和(或)pandas 构建 parquet 的列

c++ - 如何根据条件选择结构

java - Android - eclipse 中的<终止>

c - C- undefined reference

c - 为什么 y 的输出是 0.000?

c - 这个函数将数据发送到GPU进行计算?

c - memcpy() 无法正常工作

C5029 (E) 需要一个表达式

android - 数据绑定(bind)编译器错误 : "Cannot resolve type for SafeVarargs"