expected expression before '{' token

后端 未结 4 1823
清歌不尽
清歌不尽 2021-01-04 17:02

I am getting: \"error: expected expression before \'{\' token\" for the line I\'ve commented before. If the struct is already defined why would it need a \"{\" before token.

4条回答
  •  萌比男神i
    2021-01-04 17:50

    Arrays in C language are not assignable. You can't assign anything to the entire array, regardless of what syntax you use. In other words, this

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

    is not possible.

    In C89/90 you'd have to spell out your assignments line by line

    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 compound literals to assign entire structs

    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 compound literals to copy data to the array

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

    The last variant, albeit not very elegant, is the closest way to "emulate" array assignment.

提交回复
热议问题