0

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...

arunb2w
  • 1,196
  • 9
  • 28
  • @self-Thanks it doesn't shows me any error. But my doubt is, Is it necessary to declare the prototype of function? If so why while returning int it does not show any errors – arunb2w Jan 30 '14 at 11:55
  • Prototype, also include `stdlib.h` for `malloc` – David Ranieri Jan 30 '14 at 11:56
  • @arunb2w Compiler will guess the function returns an int if it can't "find" it. – this Jan 30 '14 at 11:57
  • @self - if i change the return type of main function to void, will the compiler guess the called function return type to be void without specifying the prototype. – arunb2w Jan 30 '14 at 11:59
  • @arunb2w Main should be int main... unless you have special circumstances. – this Jan 30 '14 at 12:00
  • @arunb2w: `main` **must** return `int`. – dreamlax Jan 30 '14 at 12:01
  • @dreamlax - I tried void it does not show any error. But void is also valid return type for main. – arunb2w Jan 30 '14 at 12:02
  • 1
    @arunb2w: The C standard specifically says that the return type of `main` must be `int`. If the type is not `int`, your program has undefined behaviour, meaning it may work, but there is no guarantee that it will work. For example, the compiler I am using says it is an error for `main` to return `void`. – dreamlax Jan 30 '14 at 12:03
  • @dreamlax - Thank you for clarifying my doubt by spending your precious time. But the assumption of return type to be int is based on what factor. Is the compiler considers the return type of main for its assumption or it is always int irrespective of return type of main function – arunb2w Jan 30 '14 at 12:06
  • @arunb2w: The assumption of `int` was simply how the C language was specified. Maybe because `int` is/was a very common type? Or maybe for other legacy reasons? I'm not sure, I don't really deal with old C code much. What I am certain of, is that undeclared functions are always assumed to return `int` regardless of the return type of `main`. – dreamlax Jan 30 '14 at 12:15

5 Answers5

1

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.

this
  • 5,229
  • 1
  • 22
  • 51
1

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.

pmr
  • 58,701
  • 10
  • 113
  • 156
  • Thanks it doesn't shows me any error. But my doubt is, Is it necessary to declare the prototype of function? If so why while returning int it does not show any errors – arunb2w Jan 30 '14 at 11:56
  • @arunb2w There is a very old legacy feature called: `implicit function declarations`. If you compile your code in `C99` mode, this becomes an error. Try arguments like `-std=c99` or `-std=c11` for your compiler and see. – pmr Jan 30 '14 at 11:59
1

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

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

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.

barak manos
  • 29,648
  • 10
  • 62
  • 114
0

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);
dreamlax
  • 93,976
  • 29
  • 161
  • 209