views:

176

answers:

4

I'm using CodeLite on Ubuntu and for some bizzare reason GCC keeps throwing this error whenever I try to compile code with a function that returns a pointer to a struct:

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token

Here is an example I wrote up to demonstrate this error:

#include <stdio.h>

typedef struct test_t {
    unsigned char someVar;
};

test_t* testFunc() { // GCC throws that error on this line
    return NULL;
}

int main(int argc, char **argv)
{
    return 0;
}

So unless I'm forgetting something obvious I would normally expect this code to compile on any other compiler, namely MSVC, so I'm completely confused as to why it doesn't work.

Hopefully one of you experts can please enlighten me.

Thanks!

+3  A: 

Your issue is that you are missing the "new name" for the typedef. Try changing your declaration to:

typedef struct test_t {
   unsigned char someVar;
} test_t;
Jarret Hardie
+3  A: 

You need to change your struct definition to:

typedef struct {
    unsigned char someVar;
} test_t;
Adam Maras
A: 

You should define test_t like this:

typedef struct {
 unsigned char someVar;
} test_t;
Igor Zevaka
You could define test_t like that - it is better to give it an independent tag. Since tags and types are in different namespaces, I usually use 'typedef struct test_t { ... } test_t;'. C++ doesn't need this typedef, of course.
Jonathan Leffler
So when you say namespaces do you mean that `struct test_t` is distinct from `test_t` when defined like this `typedef test_t {int blah;} test_t;`?
Igor Zevaka
+8  A: 

You defined a type named struct test_t. Later in the code you attempt to use type test_t. There's no such type in your program. The type you defined is called, again, struct test_t. This is what the compiler is trying to tell you about.

Either use the correct name of the type in the function declaration

struct test_t* testFunc() { 
  return NULL;
}

or define a short "alias" test_t for your struct test_t first

typedef struct test_t test_t;

test_t* testFunc() { 
  return NULL;
}
AndreyT
Thank you for explaining it.I haven't touched C in awhile so I had forgotten, how embarrassing. :)
JamesK89