1
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;
}
4
  • 5
    root is not a pointer. You may want struct node *list, *root; Commented Jun 19, 2011 at 15:59
  • 1
    My guess is that list=NULL; root=NULL; should probably be done inside of main. Globals are a code smell... pass them as parameters. Commented Jun 19, 2011 at 16:06
  • @pmg I did it struct node *list,*root; still same error. Commented Jun 19, 2011 at 16:07
  • By the way, since list and root are "global" variables, they will automatically be initialized to 0 (NULL) if you write struct node *list, *root;. Commented Jun 19, 2011 at 18:34

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

actually when I used struct node *list,*root; then also I had the same error.How ever I used following now typedef struct node * nodePtr; nodePtr list,root; list = NULL; root = NULL; and I still got the same error.
Some people do not like to hide the pointerness of a type behind a typedef :)
@pmg: Good point - I wondered whether to bring that out and decided not to. I don't like hiding pointer-ness inside typedefs when (as here) I'm going to dereference the type to get at the data. When the type is opaque, then I have no problem with a typedef hiding a pointer; I'm blissfully unaware of it.
@Registered User: you will continue to have problems while you try to write your 'initializer' separate from the declaration. Inside the scope of a function, you can do that; at global or file scope, you cannot. At global or file scope, you must initialize as you declare the variable.
3

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.

2 Comments

Yes you were right and I saw the error disappearing but why is that thing I mean why can I not put list=NULL outside of a function?
It's just not how C works. In some languages (including most scripting languages), you can just put statements at global scope like that. But in C (and many C-like languages), the global scope supports declaration (and even initialization) of variables, but you can't just write your whole program in the global scope. That's what main() is for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.