问题遇到的现象和发生背景
本人在编写二叉树代码时需要对决定树结点的结构体实例化,同时需要用实例化的结构体去定义一个结构体数组,用来存放结点。结果在编译时遇到了错误。
问题相关代码,请勿粘贴截图
typedef struct Treenode
{
char data;
int parent;
}PTNode;
struct Treenode treenode = {'#',1};
typedef struct
{
treenode nodes[MaxSize];
int n;
}PTree;
运行结果及报错内容
无法运行。
报错内容如下:
||=== Build: Debug in binarytree (compiler: GNU GCC Compiler) ===|
error: expected specifier-qualifier-list before 'treenode'
error: 'PTree' {aka 'struct <anonymous>'} has no member named 'n'
error: 'PTree' {aka 'struct <anonymous>'} has no member named 'nodes'
error: 'PTree' {aka 'struct <anonymous>'} has no member named 'nodes'
error: 'PTree' {aka 'struct <anonymous>'} has no member named 'nodes'
error: 'PTree' {aka 'struct <anonymous>'} has no member named 'nodes'
||=== Build failed: 6 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
我的解答思路和尝试过的方法
使用for循环对结构体数组初始化赋值。(编译成功,代码运行结果正确)
我想要达到的结果
不使用for循环对 nodes[MaxSize] 中的每个结构体进行赋值,能否直接用实例化的结构体定义 nodes[MaxSize] 数组?