v0ynich
v0ynich

Reputation: 41

#define error: expected declaration specifiers or '...' before '(' token

Do you know why I'm getting these errors out of this piece of code?

#define container_of(ptr, type, member) ({\
    const typeof( ((type *)0)->member ) *__mptr = (ptr);\
    (type *)( (void *) ( (char *)__mptr - offsetof(type,member) ) );})

error: expected declaration specifiers or '...' before '(' token
error: '__mptr' undeclared (first use in this function)

Upvotes: 2

Views: 7777

Answers (3)

user576557
user576557

Reputation: 121

"typeof" is GCC extension https://gcc.gnu.org/onlinedocs/gcc/Typeof.html

To use it one needs to specify GCC standard by using -std=gnu11 compiler option.

Upvotes: 2

v0ynich
v0ynich

Reputation: 41

Thank you for the help!

Replacing 'typeof' by a typeof with two underscores on each side solved the problem.

Upvotes: 2

peter
peter

Reputation: 1

Maybe you can replace the macro with this:

#define container_of(ptr, type, member) \ ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

I guess the way parantheses are used in your macro is the reason for the issue.

Upvotes: 0

Related Questions