带带萌新JAMMY BEAR 2022-08-20 13:41 采纳率: 100%
浏览 32
已结题

C语言:使用实例化的结构体定义数组时出错,如何解决?

问题遇到的现象和发生背景

本人在编写二叉树代码时需要对决定树结点的结构体实例化,同时需要用实例化的结构体去定义一个结构体数组,用来存放结点。结果在编译时遇到了错误。

问题相关代码,请勿粘贴截图
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循环对结构体数组初始化赋值。(编译成功,代码运行结果正确)

img

我想要达到的结果

不使用for循环对 nodes[MaxSize] 中的每个结构体进行赋值,能否直接用实例化的结构体定义 nodes[MaxSize] 数组?

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-08-20 22:00
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 9月3日
  • 已采纳回答 8月27日
  • 创建了问题 8月20日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部