gpt4 book ai didi

C中的cc编译器问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:21:21 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的代码来用 C 语言构造一棵树。下面是我的代码片段。

#include<stdio.h>

struct node
{
int data;
struct node *left;
struct node *right;
};

int main()
{
struct node *root = newNode(5);
//struct node *root = NULL; working piece
//newNode(&root,5); working piece
if(root == NULL)
{
printf("No root\n");
return 0;
}
//root->left = newNode(4);
//root->right = newNode(3);
//root->left->left = newNode(2);
//root->right->right = newNode(1);

return 0;
}

struct node* newNode(int data)
{
struct node *temp;
temp = (struct node*) malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;

return(temp);
}

当我尝试返回结构节点地址时,编译器给我错误

"rightNode.c", line 29: identifier redeclared: newNode
current : function(int) returning pointer to struct node {int data, pointer to struct node {..} left, pointer to struct node {..} right}
previous: function() returning int : "rightNode.c", line 12

但是当我评论这个 struct node* newNode(int data) 并尝试通过将结构的地址传递给下面的函数来定义一个返回 int 的函数时,它没有显示我任何错误。

int newNode(struct node **root,int data)
{
printf("Inside New Node\n");
return 0;
}

据我所知,在 C 中将结构的地址返回给调用函数是合法的。

跟编译器有关系

我在unix环境下使用cc编译器

type cc
cc is a tracked alias for /apps/pcfn/pkgs/studio10/SUNWspro/bin/cc

下面是我用来编译cc rightNode.c

的命令

任何帮助将不胜感激...

最佳答案

您需要在使用前声明一个newNode 原型(prototype)。

// somewhere after struct node definition and before first use
struct node* newNode(int);

您还需要包含 stdlib.h 以获得 malloc

关于C中的cc编译器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com