如何使用C連接頭中的多重結構?如何使用C連接頭中的多結構?
的main.c:
#include <stdio.h>
#include "main.h"
int main()
{
printf("Hello, World!");
return 0;
}
main.h:
struct Mother;
struct Sub;
/////////////////////////////
struct Mother{
sub item;
};
struct Sub{
mother item;
};
/////////////////////////////
typedef struct Mother mother;
typedef struct Sub sub;
$ gcc的-O3 -o輸出的main.c
In file included from main.c:2:0:
main.h:5:2: error: unknown type name ‘sub’
sub item;
^~~
main.h:8:2: error: unknown type name ‘mother’
mother item;
^~~~~~
如何解決問題和'struct mother'和'struct Sub'的錯誤?
新的更新: 我也嘗試作爲標題:
struct Mother;
struct Sub;
/////////////////////////////
struct Mother{
Sub *item;
};
struct Sub{
Mother *item;
};
/////////////////////////////
typedef struct Mother Mother;
typedef struct Sub Sub;
而且錯誤。
你的第一個proble是一個語法問題:'結構Mother'並不神奇'mother'請注意'struct'關鍵字和區分大小寫。你的第二個問題是一個空間問題:你不能把母親分成小孩,然後分給母親。爲此使用指向這些結構的指針。 –
不錯,可以提交一個像我的代碼沒有問題的示例代碼? –
@CPerfomance查看我的答案。你必須移動'typedef's。 – Marievi