Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning in extern declaration

Tags:

c

#include<stdio.h>
#include<stdlib.h>
#define GREY 1
#define BLACK 0
#define WHITE 2
typedef struct node * graph;
typedef struct stack * snode;

graph cnode(int data);          //cnode is to create a node for graph
void cgraph(void);
struct node {
        int data, color;
        struct node *LEFT, *RIGHT, *TOP, *DOWN;
};//this structure defines a node of the graph

struct stack {
struct stack *priv;
struct cgraph *graph_node;
};// this is to define a structure which should hold node of a structure

    extern snode sroot;

I defined a header file (declaration.h) as above and following is a c program(stack.c) I am making which would be used in the library I am developing

#include<declarations.h>
void cstack (graph temp);
void stackpush(snode stemp);
extern int stack_counter = 0;

sroot=NULL;
void cstack (graph gtemp) //cstack is to create stack
{
   snode spriv,some;
  if (stack_counter==0)
  {
   sroot=stackpush(gtemp);
    spriv=sroot;
   stack_counter++;
   }
   else{
   some=cstacknode(gtemp);
    some->priv=spriv;
    spriv=some;
  }

}

//struct stack is representing a stack
//struct node is representing a node in graph

snode  cstacknode (graph gtemp)
//this function should create a node of the stack which should be storing the graph node as a pointer
{
 snode an;
 an=(snode)malloc(sizeof(snode));
 an->graph_node=gtemp;
 an->priv=NULL;
 return an;
}

void stackpush(snode stemp)
{

}

both the above files are in same directory. I compiled the above file stack.c cc -I ./ stack.c I following warnings

stack.c:4: warning: ‘stack_counter’ initialized and declared ‘extern’
stack.c:6: warning: data definition has no type or storage class
stack.c:6: error: conflicting types for ‘sroot’
./declarations.h:21: note: previous declaration of ‘sroot’ was here
stack.c:6: warning: initialization makes integer from pointer without a cast
stack.c: In function ‘cstack’:
stack.c:12: warning: passing argument 1 of ‘stackpush’ from incompatible pointer type
stack.c:3: note: expected ‘snode’ but argument is of type ‘graph’
stack.c:12: error: void value not ignored as it ought to be
stack.c:13: warning: assignment makes pointer from integer without a cast
stack.c:17: warning: assignment makes pointer from integer without a cast
stack.c: At top level:
stack.c:27: error: conflicting types for ‘cstacknode’
stack.c:17: note: previous implicit declaration of ‘cstacknode’ was here
stack.c: In function ‘cstacknode’:
stack.c:32: warning: assignment from incompatible pointer type

I want to know when I declared a variable as extern which I have marked bold why do I get that as a warning any thoughts on that and if some one wants to share any other thing on remaining errors then let me know.

like image 422
reality displays Avatar asked Nov 24 '10 15:11

reality displays


2 Answers

While your code contains a number of rather serious and obvious errors (already covered in other answers), the warning you put into the title of your question is a completely superfluous meaningless warning. GCC compiler is notorious for issuing useless warnings. Many of those warnings seem to be derived from someone's incompetent and completely unsubstantiated belief that doing something is somehow "wrong", while in reality there's nothing wrong with it.

In your case the warning is triggered by

extern int stack_counter = 0;

declaration. Apparently, the "author" of the warning believed that extern specifier should be reserved for non-defining declarations. In this case the presence of initializer = 0 turns the declaration into a definition (and thus formally makes that extern optional). Nevertheless, there's no error in it and, in fact, extern might be quite welcome here to emphasize the fact that stack_counter is intended to be a global variable.

Again, whether you need a global variable here or not is a different question and, again, your code contains a massive number of other errors. But the warning you seem to focus your attention on is not really worth it. Just disable this warning in compiler settings (and, please, write a rude letter about it to GCC team).

like image 186
AnT Avatar answered Oct 28 '22 03:10

AnT


The extern declaration in your header file lets modules other than the one in which the variable is defined use it. If it's supposed to be defined as int stack_counter = 0 and live in stack.c, define it like that and put an extern stack_counter in the header.

On line 6 of stack.c, you didn't define a storage class for sroot. Since it's externed in the header, I assume you meant to type snode sroot=NULL.

Fix those, then implement stackpush (make sure it doesn't return void) and deal with the rest of your warnings in order. Note that in C, you have to either use forward declarations of functions (with prototypes) or define your functions before they're used. The cstack function should probably be the last function in the file.

like image 38
nmichaels Avatar answered Oct 28 '22 05:10

nmichaels