Why does GCC give me a syntax error when trying to return a structure pointer?

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

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

Here is an example I wrote 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;
}

Therefore, if I do not forget something obvious, I usually expect this code to compile on any other compiler, namely MSVC, so I'm completely confused about why it does not work.

I hope one of your experts can please me.

Thanks!

+3
source share
4 answers

struct test_t. test_t. . , , struct test_t. , .

struct test_t* testFunc() { 
  return NULL;
}

"" test_t struct test_t

typedef struct test_t test_t;

test_t* testFunc() { 
  return NULL;
}
+10

, " " typedef. :

typedef struct test_t {
   unsigned char someVar;
} test_t;
+4

struct :

typedef struct {
    unsigned char someVar;
} test_t;
+4

test_t :

typedef struct {
 unsigned char someVar;
} test_t;
0
source

Source: https://habr.com/ru/post/1723828/


All Articles