你所遇到的問題是初始化結構,當你定義它。 當使用struct name_of_struct {...};您正在定義數據類型。由於這個原因,你不能給它一個真正的價值。
struct tree{
int data;
struct tree *left,*right;
};
struct queue{
struct tree ** nodeQ;
int front;
int rear;
};
這應該做一個定義,還記得使用縮進和評論你的代碼,因爲這將導致更多的理解程序,您實際上會明白從現在開始2周。
而且我覺得其實有一個錯誤在你的代碼,不應該nodeQ是一個正常的指針,而不是一個雙指針?如果您嘗試解除引用(接受指針引用的內容)兩次,則會出現seg錯誤。
這裏是你應該如何初始化nodeQ的內容假定其單指示器和功能,我會在主函數這一次初始化。
#include <stdio.h>
#include <stdlib.h>
struct tree{
int data;
struct tree *left,*right;
};
struct queue{
struct tree * nodeQ;
int front;
int rear;
};
int main(void)
{
struct queue my_queue;
if(my_queue.nodeQ=malloc(sizeof(struct tree)))
fprintf(stderr, "Unable to allocate memory for my_queue nodeQ\n");
return 0;
}
從理解開始就不能在C中做到這一點。 – WhozCraig
→struct tree ** nodeQ; – BLUEPIXY