我正在处理一个程序太流行并将值推入堆栈并检查(查看)堆栈顶部。如果你运行我的程序你可以看到我得到“数组下标不是一个整数”的错误,我想知道这是什么意思,我应该如何继续修复这个错误。
stack.c program:
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"
static int mockUp = 42;
static int *stackTop = &mockUp;
static int stack[10];
int isstackempty() {
if(top == -1)
return 1;
else
return 0;
}
int isstackfull() {
if(top == DEFAULT_STACK_SIZE)
return 1;
else
return 0;
}
int top()
{
return *stackTop;
}
int pop( int *val)
{
if(!isstackempty()) {
val = stack[top];
top = top - 1;
return val;
printf("%d popped from stack\n", val);
} else {
printf("Error. Could not pop value as stack is empty.\n");
}
}
int push( int val)
{
if(!isstackfull()) {
top = top + 1;
stack[top] = val;
printf("%d pushed onto stack\n", val);
} else {
printf("%d could NOT be pushed as stack is full.\n", val);
}
}
int main()
{
push(1);
push(2);
push(3);
push(4);
push(5);
push(6);
push(7);
push(8);
push(9);
printf("Value at top of stack is %d\n", top());
while(!isstackempty()) {
int val = pop();
printf("Stack value popped %d\n", val);
}
return 0;
}
stack.h程序:
#define DEFAULT_STACK_SIZE 10
extern void setStackSize( int size);
extern int getStackSize();
extern void deleteStack();
extern int top();
extern int pop( int* val);
extern int push( int val);
答案 0 :(得分:1)
在这个最小的例子中你真的有很多错误。
您是否尝试过编译代码?
int top()
{
return 0;
}
int main(void)
{
top = top + 1;
}
GCC返回错误:
a.c:18:6: error: lvalue required as left operand of assignment
top = 4;
你创建堆栈的方式很糟糕。
DEFAULT_STACK_SIZE
。struct
而不是声明没有意义的随机变量(例如
mockUp
。将struct
用于此类数据结构的优势在于您
拥有对象中所需的所有信息,您无需声明
单独的变量来跟踪内部状态,而您不需要
声明全局变量。
#include <stdio.h>
typedef struct stack
{
int buffer[10];
size_t len;
} Stack;
void stack_init(Stack *stack)
{
if(stack == NULL)
return;
stack->len = 0;
}
int stack_is_full(Stack *stack)
{
if(stack == NULL)
return 0;
size_t buffer_size = sizeof stack->buffer / sizeof *stack->buffer;
if(stack->len >= buffer_size)
return 1;
return 0;
}
int stack_is_empty(Stack *stack)
{
if(stack == NULL)
return 0;
if(stack->len == 0)
return 1;
return 0;
}
// if val is NULL, user wants to pop without
// getting the value
int stack_pop(Stack *stack, int *val)
{
if(stack == NULL)
return 0;
if(stack_is_empty(stack))
{
fprintf(stderr, "stack_pop: stack is empty\n");
return 0;
}
stack->len--;
if(val)
*val = stack->buffer[stack->len];
return 1;
}
int stack_push(Stack *stack, int val)
{
if(stack == NULL)
return 0;
if(stack_is_full(stack))
{
fprintf(stderr, "stack_pop: stack is full\n");
return 0;
}
stack->buffer[stack->len++] = val;
return 1;
}
void stack_print(Stack *stack)
{
if(stack == NULL)
{
puts("(null)");
return;
}
printf("[ ");
int i;
// printing array backwards, the top first, the
// last at the bottom
for(i = stack->len - 1; i >= 0; --i)
printf("%d%c ", stack->buffer[i], i ? ',': '\0');
printf("]\n");
}
int main(void)
{
Stack stack;
stack_init(&stack);
int top_val;
if(stack_pop(&stack, &top_val))
printf("top val: %d\n", top_val);
stack_push(&stack, 1);
stack_push(&stack, 2);
stack_push(&stack, 3);
stack_push(&stack, 4);
if(stack_pop(&stack, &top_val))
printf("top val: %d\n", top_val);
stack_print(&stack);
stack_push(&stack, 5);
stack_push(&stack, 6);
stack_push(&stack, 7);
stack_push(&stack, 8);
stack_print(&stack);
if(stack_pop(&stack, &top_val))
printf("top val: %d\n", top_val);
stack_print(&stack);
stack_push(&stack, 99);
stack_push(&stack, 100);
stack_push(&stack, 101);
stack_push(&stack, 102);
stack_push(&stack, 103);
stack_push(&stack, 104);
stack_print(&stack);
for(int i = 0; i < 15; ++i)
stack_pop(&stack, NULL); // ignoring the actual value
stack_print(&stack);
return 0;
}
这是一个非常基本的堆栈,显然它可以改进。我正在努力的观点
显示是通过封装有关堆栈的所有信息
结构,很容易编写将操纵整个堆栈的函数
这是内部状态。在main
中,使用堆栈非常容易,你可以拥有
如果您愿意,可以使用多个堆栈。
这是我的例子的输出:
stack_pop: stack is empty
top val: 4
[ 3, 2, 1 ]
[ 8, 7, 6, 5, 3, 2, 1 ]
top val: 8
[ 7, 6, 5, 3, 2, 1 ]
stack_pop: stack is full
stack_pop: stack is full
[ 102, 101, 100, 99, 7, 6, 5, 3, 2, 1 ]
stack_pop: stack is empty
stack_pop: stack is empty
stack_pop: stack is empty
stack_pop: stack is empty
stack_pop: stack is empty
[ ]
答案 1 :(得分:1)
如前所述,top
是一个函数,而不是一个变量。除此之外,您似乎无法确定您是在考虑其地址还是其内容。构造top = top + 1
表明你想将它用作索引,为什么它会取消引用指针?为什么不抓住指数?
我建议完全删除mockUp
,stackTop
和top() {...}
,并在其位置添加static int top = -1
。我的阅读表明,这将按照您的预期运作。
答案 2 :(得分:0)
您使用top
作为stack
的索引,但top
被定义为函数的名称,并使用未跟()
的函数名称给你一个指向该函数的指针 - 它不是一个整数而不是一个有效的数组下标。你的代码还有其他问题,顺便说一句。
答案 3 :(得分:0)
top是一个函数,而不是整数,你需要做
val = stack[top()];
但是,您的实施还会遇到其他问题。