[英]assignment to 'int' from 'void *' makes integer from pointer without a cast
I am trying to make a linked list stack, and I followed a tutorial online, however I get this warning, and the author does not.我正在尝试制作一个链表堆栈,并且我按照在线教程进行操作,但是我收到了这个警告,而作者没有。
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef struct stack {
int data;
struct stack *next;
} stack;
stack *top;
void Initialize(stack *s){
s=malloc(sizeof(stack));
s->data=NULL;
s->next=NULL;
top=s;
return;
}
During debugging I get a segmentation fault, if I don't use malloc
in the initialize function. MINGW-W64 compiler.在调试过程中,如果我不在初始化 function.MINGW-W64 编译器中使用
malloc
,我会遇到分段错误。
The warning is due to the fact the the NULL
macro is defined (by most modern compilers) as ((void *)0)
, as it is intended to be used only for pointers.该警告是由于
NULL
宏被(大多数现代编译器)定义为((void *)0)
,因为它仅用于指针。 Assigning this value to the data
member of your structure causes the warning.将此值分配给结构的
data
成员会导致警告。
To remove this warning, use s->data=0;
要删除此警告,请使用
s->data=0;
in place of s->data=NULL;
代替
s->data=NULL;
. . The author of the tutorial is either using an older compiler or has disabled this particular warning.
本教程的作者使用的是较旧的编译器或已禁用此特定警告。
Also, the pointer ( s
) passed to your Initialize
function will be a copy of whatever variable you use as an argument when you call that function and, as such, its value is not updated in the calling code.此外,传递给
Initialize
function 的指针 ( s
) 将是您在调用 function 时用作参数的任何变量的副本,因此,它的值不会在调用代码中更新。 You haven't specified how you intend to use that function, but here is a (perhaps) better implementation:您尚未指定您打算如何使用该 function,但这里有一个(也许)更好的实现:
stack* Initialize(void) { // Don't need an argument - just return the 'answer'
stack* made = malloc(sizeof(stack));
if (made) { // Don't attempt the initialization if "malloc" failed!
made->data = 0;
made->next = NULL;
}
return made;
}
And then, when you call that function, you can assign its returned value to your 'global' top
pointer:然后,当您调用 function 时,您可以将其返回值分配给您的“全局”
top
指针:
//...
top = Initialize();
// You should check that 'top' is not NULL before continuing!
In C the macro NULL is defined like在 C 宏 NULL 定义如下
( void * )0
that is the type of the expression is a null pointer of the type void *
.即表达式的类型是 null 类型的指针
void *
。
Thus in this statement因此在这个声明中
s->data=NULL;
a pointer is assigned to an object of the type int
and the compiler issues a message saying that you are doing something wrong.一个指针被分配给一个 object 类型的
int
并且编译器发出一条消息说你做错了什么。
The function Initialize as it is written does not make a sense.写入的 function Initialize 没有任何意义。 Apart of all for example this statement
除了所有例如这个声明
s=malloc(sizeof(stack));
does not change the original pointer top
used as the function argument.不会更改用作 function 参数的原始指针
top
。
In fact the function is redundant.事实上 function 是多余的。
Nevertheless if to write such a function then it can look either like不过,如果要写这样一个 function 那么它可能看起来像
stack * Initialize( void )
{
return NULL;
}
and called like并称呼
stack *top = Initialize();
or like或者喜欢
void Initialize( stack **top )
{
*top = NULL;
}
Or for a new created node it can be declared and defined like或者对于一个新创建的节点,它可以像这样声明和定义
void Initialize( stack *s, int data, stack *next )
{
s->data = data;
s->next = next;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.