Question

I'm having what appears to be quite a common problem with some C code: assigning a structure into another structure and the compiler doesn't know what the type of the structure is. I've tried putting various typedefs and structs all over the place but still can't get the bloody thing to compile and now can longer see the woods for the trees, please help.

typedef struct Option Option; //fwd decl
typedef struct OptionsList OptionsList;
typedef struct OptionsList {
    struct Option* Option;     
    struct OptionsList* Next; // presumably this is anonymous
} OptionsList;

typedef struct Option {
    CHARPTR Name;
    CHARPTR Value;
    struct OptionList* children;
} Option;

struct OptionsList* OptionsList_Create(Option* Option);

struct Option* Options_Create(CHARPTR Name, CHARPTR Value) {
    struct Option* option = (struct Option*) malloc(sizeof(struct Option));
    **option->children = OptionsList_Create(NULL);** // <- ARRRRRGGGGGHHHHHH!!!!!!!
    return option;
}

The warning is coming from the line:

option->children = OptionsList_Create(NULL);

and the warning is

warning C4133: '=' : incompatible types - from 'OptionsList *' to 'OptionList *'

Vs2012 update 2012 - and the project is being compiled as C (/TC)

Many thanks.

Was it helpful?

Solution

See the error:

incompatible types - from from 'OptionsList *' 
                            to 'OptionList *'

Therefore, in Option structure:

struct OptionList* children;

Should be:

struct OptionsList* children;
-------------^---------------

OTHER TIPS

The following should compile. Please name the typedef and name in struct deceleration different. typedef helps you create a short name not forward declare. You are using same name for the typdef and struct decleration.

 struct OptionsList;// forward declare
typedef struct SOption {
    CHARPTR Name;
    CHARPTR Value;
    struct OptionsList* children;
} Option;

typedef struct OptionsList {
    Option* Option;     
    struct OptionsList* Next; // presumably this is anonymous
 } OptionList;

 OptionList* OptionsList_Create(Option* Option);

 Option* Options_Create(CHARPTR Name, CHARPTR Value) {
      Option* option = (Option*) malloc(sizeof(struct Option));
      option->children = OptionsList_Create(NULL);** // <- ARRRRRGGGGGHHHHHH!!!!!!!
      return option;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top