Registered User
Registered User

Reputation: 5361

structure pointer declaration giving error

struct node {
int data;
struct node  *next;
};
struct node * list,root;
list=NULL;
root=NULL;

Above is what I am trying to create in my program and my program when I compile gives error

kbc.c:8: warning: data definition has no type or storage class
kbc.c:8: error: conflicting types for ‘list’
kbc.c:7: note: previous declaration of ‘list’ was here
kbc.c:8: warning: initialization makes integer from pointer without a cast
kbc.c:9: warning: data definition has no type or storage class
kbc.c:9: error: conflicting types for ‘root’
kbc.c:7: note: previous declaration of ‘root’ was here
kbc.c:9: warning: initialization makes integer from pointer without a cast

Here is the full program if some one wants to see I am just making a program to write a link list

#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node  *next;
};
struct node * list,root;
list=NULL;
root=NULL;
struct node * create_node(int );
int main ()
{
int i,j,choice;
printf("Enter a number this will be root of tree\n");
scanf("%d",&i);
printf("Give us choice 1 to enter more numbers 0 to quit\n");
scanf("%d",&choice);
switch (choice)
        {
        case 1:break;

        case 2:break;
        }
}
// end of function main
struct node * create_node(int data)
{
struct node *temp;
t    emp = (struct node *)malloc(sizeof(struct node));
return temp;
}

Upvotes: 1

Views: 2149

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753475

Part of the problem is that you have declared one pointer and one structure. Your declarations are equivalent to:

struct node *list;
struct node  root;

This is why the '*' indicating a pointer belongs with the variable name (declarator in standardese) and not with the type.

The other part of the problem is that you can only initialize a variable at global scope with a (constant) initializer; you cannot write assignments at global scope.

There are multiple ways to fix this:

typedef struct node *NodePtr;
NodePtr list = NULL, root = NULL;

struct node *list = NULL, *root = NULL;

struct node *list = NULL;
struct node *root = NULL;

Note that one of the many reasons for not using a #define in place of typedef is that:

#define NodePtr struct node *        // BAD!
NodePtr list = NULL, root = NULL;    // BAD!

creates exactly the erroneous situation in the question.

Addressing some of the error messages:

kbc.c:8: warning: data definition has no type or storage class

This is referring to the line:

list = NULL;

Because you are writing at global scope, this is interpreted as a variable definition, with no explicit type. This is horrid style nowadays, but was allowed in original (pre-standard) C, using the 'implicit-int' rule to infer the type - which also explains some of the subsequent messages. The 'implicit-int' interpretation was obsolescent in C89 and not officially supported by C99, but compilers still recognize it (with a warning at least in C99 mode).

So, this is interpreted as:

int list = NULL;

These messages are now explained:

kbc.c:8: error: conflicting types for ‘list’
kbc.c:7: note: previous declaration of ‘list’ was here

You have written code that the compiler must interpret as a second declaration of list.

kbc.c:8: warning: initialization makes integer from pointer without a cast

And this is explained; since the second list is an int and NULL is a null pointer constant in this implementation (likely #define NULL ((void *)0)), there is a conversion from a pointer to an integer without a cast.

The second set of warnings apply to analogously to root.

Upvotes: 3

John Zwinck
John Zwinck

Reputation: 249103

This is not OK at global scope:

struct node * list,root;
list=NULL;
root=NULL;

Try this:

struct node *list=NULL, *root=NULL;

The reason is that you can't put statements to execute (like list=NULL) outside of a function.

Upvotes: 3

Related Questions