我想编写一个简单的代码来构建一个C语言的树。以下是我的代码片段。C编译器问题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中返回结构地址到调用函数是合法的。
这与编译器有关。
我使用cc编译在UNIX环境
type cc
cc is a tracked alias for /apps/pcfn/pkgs/studio10/SUNWspro/bin/cc
下面是把我用来编译cc rightNode.c
任何帮助,将不胜感激命令
@自我由于它不显示我的任何错误。但我的疑问是,是否有必要声明函数的原型?如果是这样,为什么而返回int它不显示任何错误 – arunb2w
原型,还包括对''malloc' –
@ stdlib.h' arunb2w编译器会猜测该函数返回一个int,如果它不能“发现”它。 – this