-1

So I have the following located in a .h file

typedef struct Supply {
      char* name;
      struct Supply* nextSupply;
      int quantity;
 } Supply;

And the following

typedef struct Location {
      Supply* firstSupply;
 } Location;

And I want to use it in a snippet such as this in a c file where the h file is included

void snippet(Location* location, Supply* incoming) {
    Supply* first = location->firstSupply;
    Supply* check = first->nextSupply;
    if(!strcmp(first->name,incoming->name) {
          *some stuff*
    }
    *while loop checking entire linked list*
}

Why is it that I am told by gcc -Wall -pedantic that I am assigning from invalid pointer types. I understand that in definition of Supply I must refer to the nextSupply as a struct Supply* but I thought after the definition is finished then Supply* == struct Supply*

  • 2
    This doesn't look like it's your actual code, e.g. `if(!strcmp(first->name,incoming->name) {` will not even compile as-is ? Please copy and paste *actual code* rather than an approximation (although it's OK to trim stuff out of course). – Paul R Oct 15 '15 at 08:06
  • 1
    I think your idea about it is right, you must have another type of error. But since you are not showing us your whole code, we can't figure this out. – Jens Gustedt Oct 15 '15 at 08:43
  • I very much doubt that as the c compiler identifies only the lines I have shown to you as evidence of assigning to an invalid pointer. – user2949883 Oct 15 '15 at 08:58

1 Answers1

0

I think this is related to your problem. The very fisrt answer should fix the deal.

How to define a typedef struct containing pointers to itself?

From my perception it is a struct defenition scope issue. Meaning that, the struct definition must be within scope before variables of that type can be declared.

So the work around would be a forward declaration, which is a declaration of your struct, which you would define later in your code:

typedef struct Supply Supply ;

typedef struct Supply {
      char* name;
      struct Supply* nextSupply;
      int quantity;
 } Supply;
Community
  • 1
  • 1
lazyneuron
  • 537
  • 5
  • 12