Oliver Spryn
Oliver Spryn

Reputation: 17368

C Compiler Pointless Errors?

I have the following code written in 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;
     }

Any idea why the code compiles to give these errors:

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'

Removing the strcpy() and inputting UDS1 and UDS2 directly into the struct works.

Upvotes: 1

Views: 108

Answers (1)

cdarke
cdarke

Reputation: 44434

You are almost certainly using an early compiler standard, like C89, which does not allow mixed declarations and code. You need to declare item near the start of the code block. Something like this:

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

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

Since you are only placing the pointers into the struct, the initialisation can be done before the strcpy. But you must declare UDSData after the two char arrays.

Upvotes: 2

Related Questions