关于指针函数赋值时的警告:赋值时将整数赋给指针,未作类型转换

czg0002 czg0002 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
打算用栈写一个hanoi的解答,可是在基础编译的时候却出现一下的警告:
hanoi_stack.c:7: 警告: 赋值时将整数赋给指针,未作类型转换
hanoi_stack.c:8: 警告: 赋值时将整数赋给指针,未作类型转换
hanoi_stack.c:9: 警告: 赋值时将整数赋给指针,未作类型转换
但是运行的结果是预期的。
请问这个警告是怎么出现的?仔细又看了一下代码,感觉右边返回的是指针变量啊。
多谢大虾赐教。
源代码:(问题部分用红色标出)
hanoi_stack.c:
#include <stdio.h>
#include "stack.h"
#define HANOI_SIZE 8
int main()
{
Stack hanoi_left = NULL, hanoi_mid = NULL, hanoi_right = NULL;
hanoi_left = CreatStack();
hanoi_mid = CreatStack();
hanoi_right = CreatStack();

int tmp;
tmp = CreatStack();

printf("Please enter the size of hanoi:\n");
int hanoi_size;
scanf("%d", &hanoi_size);
int i;
for (i = 1; i <= hanoi_size ; ++i) {
Push(hanoi_size-i+1, hanoi_left);
}

PrintStack(hanoi_left);
PrintStack(hanoi_mid);
PrintStack(hanoi_right);

return 0;
}

stack.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct Node {
ElementType element;
PtrToNode next;
};
int IsEmpty(Stack S)
{
return S->next == NULL;
}

Stack CreatStack(void)
{
Stack S;
S = (Stack)malloc( sizeof(struct Node) );
if (S == NULL) {
fprintf(stderr, "Out of space!!");
exit(-1);
}
S->next == NULL;
return S;
}


void MakeEmpty(Stack S)
{
if (S == NULL)
fprintf(stderr, "Must use CreateStack first");
else
while( !IsEmpty(S))
Pop(S);
}

void Push(ElementType X, Stack S)
{
PtrToNode TmpCell;
TmpCell = malloc( sizeof(struct Node) );
if ( TmpCell == NULL) {
fprintf(stderr, "Out of space!!");
exit(-1);
} else {
TmpCell->element = X;
TmpCell->next = S->next;
S->next = TmpCell;
}
}

ElementType Top(Stack S)
{
if (!IsEmpty(S))
return S->next->element;
else {
fprintf(stderr, "Empty Stack!!");
return 0;
}
}

void Pop(Stack S)
{
PtrToNode first_cell;
if (IsEmpty(S))
fprintf(stderr, "Empty Stack");
else {
first_cell = S->next;
S->next = S->next->next;
free(first_cell);
}
}

ElementType TopAndPop(Stack S)
{
ElementType tmp_element;
PtrToNode first_cell;
if (IsEmpty(S)){
fprintf(stderr, "Empty Stack");
return -1;
} else {
tmp_element = S->next->element;
first_cell = S->next;
S->next = S->next->next;
free(first_cell);
return tmp_element;
}
}

void DisposeStack(Stack S)
{
MakeEmpty(S);
free(S);
}

void PrintStack(Stack S)
{
PtrToNode current_cell;
current_cell = S->next;

while (current_cell != NULL) {
printf("<-%d", current_cell->element);
current_cell = current_cell->next;
}
printf("\n");
}

头文件stack.h:
#ifndef _STACK_H
typedef int ElementType;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

int IsEmpty(Stack S);
Stack CreateStack(void);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
#endif
...全文
评论 8
收藏 已收藏
分享
返回顶部
czg0002
新手乐园 33,315 成员 41,769 内容