-1

I define a struct in file s.h

struct Scommand {
    int a;
    struct Scommand next;
}

and in my main.c I include s.h and have this code

struct Scommand *commandList=0;
struct SCommand *thisCommand;
thisCommand = commandList;

for some reason I always get the error "Assignment form incompatible pointer types" on the last line. Can someone explain why? This seems pretty simple

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
TSG
  • 4,242
  • 9
  • 61
  • 121

1 Answers1

1

There are 2 unrealated problems here:

Definition of the initial structure should look like:

struct Scommand {
    int a;
    struct Scommand *next;
}

Without that you are trying to insert the whole structure into itself. This makes type definition recursive and this not allowed.

Second point. C and C++ have a concept of forward declaration:

struct Scmd *someCommand;

It might be surprising, but this line makes 2 things:

  • Creates forward declaration for Scmd structure in the names table.
  • Defines the variable someCommand.

In spite of the fact that this line looks like definition of the variable, in reality there are 2 definitions here. Now returning back to your code:

struct Scommand *commandList=0;
struct SCommand *thisCommand;
thisCommand = commandList;

The names of the structures are different because all names in C are case sensitive. The first one is already defined. The second one SCommand is defined in place. This is independent structure and it is not related to your original structure. This is the reason for the error. I agree that in this particular case the error is not very self explanatory. But well, it may appear in other cases where it might sound more appropriate.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51