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

czg0002 2008-12-02 03:18:40
打算用栈写一个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
...全文
1879 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
czg0002 2008-12-08
  • 打赏
  • 举报
回复
看看stack.h
我是把Stack声明为指针了呀。
在调试的时候我也是查看Stack->成员变量名。
ningweidong 2008-12-05
  • 打赏
  • 举报
回复
S是函数体的临时变量,它不能直接返回
ningweidong 2008-12-05
  • 打赏
  • 举报
回复

Stack CreatStack(void)
{
Stack S;
S = (Stack)malloc( sizeof(struct Node) ); //一个指针怎么能赋给一个结构呢
if (S == NULL)
{
fprintf(stderr, "Out of space!!");
exit(-1);
}
S->next == NULL; //不是指针,不能用->要用. ==也写错了 S.next = NULL;
return S;
}

Stack* CreatStack(void)
{
Stack *p = NULL;

p = (Stack*)malloc( sizeof(struct Node) );
if (p == NULL)
{
fprintf(stderr, "Out of space!!");
exit(-1);
}
p->next == NULL;
return p;
}
czg0002 2008-12-02
  • 打赏
  • 举报
回复
恩那,这个确实是我敲错了。改了。

我是用ubuntu下的gcc编译的,我怀疑是不是我的gcc的版本的问题。
yellowhwb 2008-12-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cyj626 的回复:]
S = (Stack*)malloc( sizeof(struct Node) );
[/Quote]

这个不对,Stack本身就是一个指针了
yellowhwb 2008-12-02
  • 打赏
  • 举报
回复
我编译了lz的代码,VC6,好像没有警告啊,只是CreatStack(void)函数中S->next == NULL;这句应该有问题,S->next = NULL;这样才对!
cyj626 2008-12-02
  • 打赏
  • 举报
回复
S = (Stack*)malloc( sizeof(struct Node) );
ChamPagneZ 2008-12-02
  • 打赏
  • 举报
回复

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

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧