代码没有报错但不知道问题出现在哪里,没有输出结果就直接跳出了
```c
#include
#include
#include
#include
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct {
ElemType* base;
ElemType* top;
int stacksize;
}SqStack;
void InitStack(SqStack* S) {
S->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
if (!S->base)exit(OVERFLOW);
S->base = S->top;
S->stacksize = MAXSIZE;
}
int Push(SqStack* S, ElemType e) {
if (S->top - S->base >= S->stacksize) {
return ERROR;
/*S->base = (ElemType*)realloc(S->base, (S->stacksize + MAXSIZE) * sizeof(ElemType));
S->top = S->base + S->stacksize;
S->stacksize = S->stacksize + MAXSIZE;*/
}
*S->top++ = e;
return OK;
}
int Pop(SqStack* S, ElemType e) {
if (S->top == S->base) {
return ERROR;
}
e = *--S->top;
printf("%d", e);
return OK;
}
int Empty(SqStack* S) {
if (S->base == S->top) {
return OK;
}
else
{
return ERROR;
}
}
int main() {
SqStack* S;
SqStack a;
S = &a;
int N;
ElemType e = 0;
InitStack(&S);
printf("请输入一个非负整数\n");
scanf_s("%d", &N);
system("pause");
while (N) {
Push(&S, N % 8);
N = N / 8;
}
system("pause");
while (!Empty(S))
{
Pop(&S, &e);
system("pause");
}
return 0;
}
```
这段代码,我看了一下,只需要改一些地方即可,我改好了。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct {
ElemType* base;
ElemType* top;
int stacksize;
} SqStack;
void InitStack(SqStack* S) {
S->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
if (!S->base) exit(OVERFLOW);
S->top = S->base;
S->stacksize = MAXSIZE;
}
int Push(SqStack* S, ElemType e) {
if (S->top - S->base >= S->stacksize) {
return ERROR;
}
*S->top++ = e;
return OK;
}
int Pop(SqStack* S, ElemType* e) {
if (S->top == S->base) {
return ERROR;
}
*e = *--S->top;
return OK;
}
int Empty(SqStack* S) {
if (S->base == S->top) {
return OK;
}
else {
return ERROR;
}
}
int main() {
SqStack* S;
SqStack a;
S = &a;
int N;
ElemType e = 0;
InitStack(S);
printf("请输入一个非负整数\n");
scanf_s("%d", &N);
while (N) {
Push(S, N % 8);
N = N / 8;
}
while (!Empty(S))
{
Pop(S, &e);
printf("%d", e);
}
return 0;
}
供参考:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct {
ElemType* base;
ElemType* top;
int stacksize;
}SqStack;
void InitStack(SqStack* S) {
S->base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
if (!S->base)exit(OVERFLOW);
S->top = S->base; //S->base = S->top; 修改
S->stacksize = MAXSIZE;
}
int Push(SqStack* S, ElemType e) {
if (S->top - S->base >= S->stacksize) {
return ERROR;
/*S->base = (ElemType*)realloc(S->base, (S->stacksize + MAXSIZE) * sizeof(ElemType));
S->top = S->base + S->stacksize;
S->stacksize = S->stacksize + MAXSIZE;*/
}
*S->top++ = e;
return OK;
}
int Pop(SqStack* S, ElemType* e) { //修改 ElemType e 修改
if (S->top == S->base) {
return ERROR;
}
*e = *--S->top; //e = *--S->top; 修改
printf("%d", *e); //printf("%d", e); 修改
return OK;
}
int Empty(SqStack* S) {
if (S->base == S->top)
return OK;
else
return ERROR;
}
int main() {
SqStack* S;
SqStack a;
S = &a;
int N;
ElemType e = 0;
InitStack(S); //InitStack(&S); 修改
printf("请输入一个非负整数\n");
scanf_s("%d", &N);
//system("pause"); 修改
while (N) {
Push(S, N % 8); //Push(&S, N % 8);修改
N = N / 8;
}
//system("pause"); 修改
while (!Empty(S))
{
Pop(S, &e); //Pop(&S, &e); 修改
//system("pause"); 修改
}
return 0;
}
这段代码有以下几个问题:
(1)在 InitStack 函数中,赋值 S->base = S->top 应该改为 S->top = S->base,因为 S->top 指向的是栈顶,初始化时应该指向栈底。
(2)在 Push 函数中,应该先判断栈是否已满,然后再进行入栈操作。
(3)在 Pop 函数中,传入的参数 e 应该是指针类型,因为需要修改 e 的值。而且,弹出栈顶元素后应该将其释放掉,避免内存泄漏。
(4)在 main 函数中,调用 InitStack 函数时,应该传入的是地址而不是指针本身。而且, S 已经是指针类型,不需要再用 & 取地址。
我们看代码可以更容易理解。
int a[3][4];
int(*p)[4];//数组指针,p先和*结合,说明p是一个指针变量,然后指着指向的是一个大小为4个整型的数组。所以p是一个指针,指向一个数组叫指针数组。
//这里要注意:[]的优先级要高于*号的,所以必须加上()来保证p先和*结合。
一个数组,叫数组指
int *q[3];//指针数组
p = a;
for (int i = 0; i < 3; ++i)
{
q[i] = a[i];数组名一定不能放在左值
}
int a[3][4];//a(int(*)[4])a[i](int *) a[i][j](int)
int b[10];//b(int *) b[i](int)
从二维数组来理解,首先先介绍几个概念。
a[0] | ||||
a[1] | ||||
a[2] |