C将结构分配给结构,现在变得非常烦人

时间:2013-08-19 14:48:26

标签: c struct anonymous-types

我遇到了一些C代码的常见问题:将结构分配到另一个结构中,编译器不知道结构的类型是什么。我已经尝试过各种各样的typedef和结构,但仍然无法获得编译的血腥东西,现在可以更长时间地看到树林,请帮助。

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;
}

警告来自以下行:

option->children = OptionsList_Create(NULL);

并且警告是

  

警告C4133:'=':不兼容的类型 - 从'OptionsList *'到   'OptionList *'

Vs2012更新2012 - 该项目正在编译为C(/ TC)

非常感谢。

2 个答案:

答案 0 :(得分:1)

查看错误:

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

因此,在Option结构中:

struct OptionList* children;

应该是:

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

答案 1 :(得分:0)

以下内容应该编译。请将structdef和name命名为struct deceleration。 typedef可以帮助您创建一个短名称而不是前向声明。您对typdef和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;
 }