tags:

views:

994

answers:

3

Is there an easy explanation for what this error means?

request for member '*******' in something not a structure or union

I've encountered it several times in the time that I've been learning C, but I haven't got a clue as to what it means.

A: 

The compiler is telling you that you are trying to access a member of a structure, but the structure does not have a member with that name. For example:

struct {
    int   a;
    int   b;
    int   c;
} foo;
foo.d = 5;

There is no member of struct foo called 'd', so it will give you this error.

Graeme Perrow
Actually, I think this answer is wrong. This error will give another message, namely that the struct has no member named 'd'.
Thomas Padron-McCarthy
Definitely wrong, sorry, @Graeme. The clue is "in something not a structure or union" which means you're 'dotting' a non-struct/union type, _not_ trying to access a non-existent member (which gives something like "error: ‘struct tFoo’ has no member named ‘d’".
paxdiablo
@paxdiablo: so I'm trying to use something as a struct/union that is not in fact a struct or a union?
Pieter
@Pieter: yes, usually it's dotting a pointer (pVar.field) rather than de-referencing properly (pVar->field) but, as Thomas points out in his answer (and I in mine before I deleted it (since I've hit my daily cap anyway, Thomas gets my vote)), you can get the same effect with "int i; int j = i.x;" - that's because neither the pointer pVar nor the integer i is a struct/union.
paxdiablo
Graeme Perrow
+1  A: 

It also happens if you're trying to access an instance when you have a pointer, and vice versa:

struct foo
{
  int x, y, x;
};

struct foo a, *b = a;

b.x = 12;  /* This will generate the error, should be b->x or (*b).x */
unwind
I'll bet this is the actual problem. It still bites me on occasion, *especially* if someone has typedef'd a pointer type.
John Bode
+2  A: 

You are trying to access a member of a structure, but in something that is not a structure. For example:

struct {
    int a;
    int b;
} foo;
int fum;
fum.d = 5;
Thomas Padron-McCarthy