指针转换后指向其它类型在 main 函数中

来源:百度知道 编辑:UC知道 时间:2024/05/02 09:55:17
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
#include<stdio.h>
#include<malloc.h>
void InitStack(SqStack *S)
{S->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!S->base) exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;

}
void Push(SqStack *S,SElemType e)
{if(S->top-S->base>=S->stacksize)
{S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S->base) exit(0);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;}
*S->top=e;
S->top++;
}
void Pop(SqStack *S,SElemType *e)
{*e=*S->top;
S->top--;

}
void main()

是指针用得不对。
在主函数中
SElemType *e; 是定义一个指针

Pop(&S,&e); 传入的实参是e的地址,也就是指针的地址

void Pop(SqStack *S,SElemType *e)
{*e=*S->top;
S->top--;

}

*e=*S->top; 这一句
操作的是结果是把*S->top 给了 指针(e),类型不对。

应改为

void Pop(SqStack *S,SElemType **e)/**** 这就可以了****/
{ *e=*S->top;
S->top--;

}

另外,
1,入栈没有对数据进行处理。
2,出栈只出了一个,没有循环处理。