Expected expression before the character '{'

I get: "error: expected expression before" {"token" for a string that I already commented on. If the structure is already defined, why does it need "{" before the marker. Thanks for any help you can provide.

struct sdram_timing { u32 wrdtr; u32 clktr; }; int calibration(void); unsigned char read_i2c_cal(void); static unsigned int eepcal[15]; main() { DQS_autocalibration(); } int calibration(void) { struct sdram_timing scan_list[30]; read_i2c_cal(); if(eepcal[0] == 0){ scan_list = {{eepcal[1], eepcal[2]}, {-1, -1}}; // <-- PROBLEM LINE } else { //foo } return 0; } unsigned char read_i2c_cal(void) { eepcal[0] = 0; eepcal[1] = 02; eepcal[2] = 03; } 
+6
source share
4 answers

The error is that you cannot assign an array in a way that only works to initialize it.

 int arr[4] = {0}; // this works int arr2[4]; arr2 = {0};// this doesn't and will cause an error arr2[0] = 0; // that OK memset(arr2, 0, 4*sizeof(int)); // that is too 

So, applying this to your specific example:

 struct sdram_timing scan_list[30]; scan_list[0].wrdtr = 0; scan_list[0].clktr = 0; 

or you can use memset the same way, but instead of sizeof (int) you need the size of your structure. This does not always work ... but given your structure it will be.

+14
source

You can use only the list of initializers in the variable declaration, and not after the fact.

+1
source

The list of initializers can only be used to initialize the array. You cannot use it afterwards.

However, if you use GCC , you can use the Compound Literal extension:

 scan_list = (struct sdram_timing[30]){{eepcal[1], eepcal[2]}, {-1, -1}}; 

You may need to change the scan_list type to struct sdram_timing *

+1
source

C arrays cannot be assigned. You cannot assign anything to the entire array, no matter what syntax you use. In other words, this

 scan_list = { { eepcal[1], eepcal[2] }, {-1, -1} }; 

impossible.

In C89 / 90 you will need to specify your line items

 scan_list[0].wrdtr = eepcal[1]; scan_list[0].clktr = eepcal[2]; scan_list[1].wrdtr = -1; scan_list[1].clktr = -1; 

In modern C (post-C99), you can use complex literals to assign entire structures

 scan_list[0] = (struct sdram_timing) { eepcal[1], eepcal[2] }; scan_list[1] = (struct sdram_timing) { -1, -1 }; 

Finally, in modern C, you can use memcpy and complex literals to copy data to an array

 memcpy(scan_list, (struct sdram_timing[]) { { eepcal[1], eepcal[2] }, {-1, -1} }, 2 * sizeof *scan_list); 

The latter option, although not very elegant, is the closest way to "emulate" the purpose of the array.

+1
source

All Articles