Question

I am trying to write a simple code to construct a tree in C language. Below is my code snippet.

#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);
}

When I try to return the structure node address, the compiler gives me the error

"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

But when I comment this struct node* newNode(int data) and try to define a function that returns int by passing the address of the structure to the function like below, it does not shows me any error.

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

As far I know, it is legal in C to return the address of the structure to the calling function.

It is something to do with the compiler.

I am using cc compiler in unix environment

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

Below is the command I used to compile cc rightNode.c

Any help would be appreciated...

Was it helpful?

Solution

You need to declare a newNode prototype before you use it.

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

You also need to include stdlib.h to get malloc.

OTHER TIPS

Put this struct node* newNode(int data) above the code and include stdlib.h.

You need a function prototype if you are going to use a function before you declare it. Also malloc is defined in stdlib.h.

There is no function prototype visible when you call struct node *root = newNode(5); so the compiler gets confused.

When the compiler cannot find a function declaration, it assumes that such function exists, but returning int. Declare struct node* newNode(int data); before you call newNode(...) in main.

In older versions of C, you did not need to declare a function before using it. In older C, functions that are not declared are assumed to return int and accept an unspecified number of arguments. This is the reason you are getting the error, because the compiler assumes the newNode function returns int, rather than struct node *.

In modern C (C99 and newer), you can no longer do this. You must declare functions before they are used. Some compilers still allow the old behaviour and warn against it, but a strictly conforming C99 program cannot use a function without declaring it first.

In your case, you should put the following line of code before your main function. This tells the compiler about the newNode function and how it should be called:

struct node *newNode(int);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top