-2
struct test {
  int id_number;
  int age;
};

test *tester() {
    struct test *test1 = malloc(sizeof(test));
    test1->id_number = 10;
    test1->age = 1;
    return test1;
}

int main()
{
    test *tester = function();
    printf("%d %d \n",tester->id_number tester->age );
}

So I'm trying to do some testing with malloc() and structs, but when I try to run my tester code I get an error saying unknown type test, however i am defining the struct test.

Bob Doe
  • 3
  • 4

2 Answers2

0

When referencing a struct type, you need to use the struct keyword:

struct test *tester() {
   ...
}

If you want to be able to use test as a type, you need a typedef:

typedef struct test {
  int id_number;
  int age;
} test;
dbush
  • 205,898
  • 23
  • 218
  • 273
0

The type is called struct test, unless you add a typedef:

typedef struct test test;

You can combine the two, this is very common:

typedef struct {
  int id_number;
 int age;
} test;

This is very common; notice that the "struct tag" can be omitted.

unwind
  • 391,730
  • 64
  • 469
  • 606