gpt4 book ai didi

c - 外部声明中的警告

转载 作者:太空狗 更新时间:2023-10-29 16:39:22 25 4
gpt4 key购买 nike

#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.ccc -I ./stack.c I 遵循警告

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 时,我已将其标记为粗体,为什么我会得到它作为警告,对此有任何想法,如果有人想分享关于剩余错误的任何其他事情,请告诉我。

最佳答案

虽然您的代码包含许多相当严重和明显的错误(已包含在其他答案中),但您在问题标题中提出的警告完全是多余的、毫无意义的警告。 GCC 编译器因发出无用的警告而臭名昭著。许多警告似乎源自某人的无能且完全没有根据的信念,即认为做某事在某种程度上是“错误的”,而实际上并没有错。

在您的情况下,警告是由

触发的
extern int stack_counter = 0;

声明。显然,警告的“作者”认为 extern 说明符应该保留用于非定义声明。在这种情况下,初始化程序 = 0 的存在将声明变成了定义(因此正式地使 extern 成为可选的)。尽管如此,它没有错误,事实上,extern 在这里可能非常受欢迎,以强调 stack_counter 旨在成为全局变量这一事实。

同样,这里是否需要全局变量是一个不同的问题,而且您的代码同样包含大量其他错误。但是您似乎将注意力集中在警告上并不值得。只需在编译器设置中禁用此警告(并且,请就此给 GCC 团队写一封粗鲁的信)。

关于c - 外部声明中的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com