tags:

views:

189

answers:

3

I haven't used C in a long time. What's going on here?


wOCR.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
wOCR.c:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token

on this code:

struct wOCR_matchGrid {
  char character;
  int * data;
};

struct wOCR_matchGrid alphaMatch[26];

alphaMatch[0].character = 'A'; /*8*/
alphaMatch[0].data = {         /*9*/
                     0, 1, 0,
                     1, 0, 1,
                     1, 1, 1,
                     1, 0, 1   
                     };
+1  A: 

You should do these assignments inside function body.

Pavel Shved
DUH!!!! thnx lotz. I'm so crusty on my C
woxorz
A: 

You can do it like this, but it's messy as hell:

struct wOCR_matchGrid {
  char character;
  int data[12];
};

struct wOCR_matchGrid alphaMatch[26] = 
{
  {'A', {0, 1, 0,
           1, 0, 1,
           1, 1, 1,
           1, 0, 1}},
   {'B', {0, 1, 0,
           1, 0, 1,
           1, 1, 1,
           1, 0, 1}},
   /* etc */
};

Note that data cannot be arbitrarily sized in this case.

Tyler McHenry
+2  A: 

First, you should do it inside a function body. Then, the syntax of using the braces is illegal, because you assign them to a pointer, not to an array or struct. That syntax is also only valid when initializing, not during assignment.

The code is probably using compound literals, and some programmer removed the necessary type name:

void some_function(void) {
    struct wOCR_matchGrid alphaMatch[26];

    alphaMatch[0].character = 'A'; /*8*/
    alphaMatch[0].data = (int[]){         /*9*/
                         0, 1, 0,
                         1, 0, 1,
                         1, 1, 1,
                         1, 0, 1   
                         };

};

Note the parenthesized int[] i added. That will tell GCC to create an unnamed array living for the lifetime of that function invocation, and assign the address of it to the .data pointer. This is not a cast in this case, but it's essential for this syntax to work and to create an array compound literal. These compound literals, however, are a C99 feature, and don't work in every compiler.

Johannes Schaub - litb
Granted, compound literals do not work with all compilers (notably not with MSVC!), but they do work in GCC unless you tell it not to allow it with `-std=c89` or some similar option.
Jonathan Leffler