srib
srib

Reputation: 168

request for member n something not a structure or union

I have a struct defined like this:

typedef struct spi_device device_t;

struct spi_device {
  int a;
  int b;
  int c;
};

defined in include file.

And in source file,

static device_t my_main_dev = 
{
  .a = 2,
  .b = 3,
  .c = 4,    
};

I have made sure that the include file is included in the source file.

But when I compiled this code I am getting the error 'request for member 'a' in something not a structure or union'. I am not able to rectify this issue.

I have read few posts which talk about this problem, but none of them say what we can do when we face an error during initialization of structure object. Any help in this regard is appreciated.

Upvotes: 0

Views: 242

Answers (2)

chqrlie
chqrlie

Reputation: 144695

You did not provide a compilable source file that exhibits the problem. The diagnostic might pertain to some other part of your source file than what you posted.

If the warning really points to the posted code, here are some possible explanations:

  • your compiler does not support C99 extensions. These are quite rare today, but some old compilers targeting the embedded processors are clunky and like old monkeys, cannot be taught new tricks.
  • your compiler is configured to reject c99 extensions (with -std=c89 or -ansi).

To work around these limitations, you can use the classic syntax for structure initializers:

static device_t my_main_dev = { 2, 3, 4 };

Upvotes: 1

srib
srib

Reputation: 168

It was a silly bug. I had missed a comma for one of the fields. Putting it back resolved the compiler error. Sorry for the trouble.

Upvotes: 0

Related Questions