【发布时间】:2010-11-24 15:42:17
【问题描述】:
#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;
我在上面定义了一个头文件(declaration.h),下面是一个c程序(stack.c) 我正在制作的将在我正在开发的库中使用
#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)
{
}
以上两个文件都在同一个目录中。
我编译了上面的文件stack.c
cc -I ./ stack.c我遵循警告
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
我想知道我何时将一个变量声明为 extern,我已将其标记为粗体,为什么我会将此作为警告,对此有任何想法,如果有人想就剩余错误分享任何其他内容,请告诉我。
【问题讨论】:
-
我相信 nmichaels 的答案(在撰写本文时排在第二位)是正确答案:在头文件中声明您的 extern 并在您的 c 文件中定义它而不使用 extern。如果其他文件想要使用这个变量,他们将必须包含头文件并与 c 文件链接。这样,您还可以在一个地方声明类型,并且编译器可以检查不匹配(我不确定链接器是否会注意到两个文件是否以不同方式声明了相同的 extern)。
标签: c