I have problem in push function. My code is
struct stack_t{
DATA_TYPE size;
DATA_TYPE top;
DATA_TYPE *arr;
}
void push(stack_t* s, DATA_TYPE item) {
if (is_full(s) == true ) {
exit(1);
}
else {
s->arr[++(s->top)] = item;
}
}
The line s->arr[++(s->top)] = item; has the error.(Bold part especially) It says read access error. I found that I have to write my code in address but I write my code in value. But I have no idea how to change my code. How I can express Struct array address?
first,
size
should be an int value instead ofDATA_TYPE
. Then in push, you should uses->arr[++(s->size)]
. But still, you need to make sure the stack and thearr
are properly initialized/allocated. And I wonder how you want to implement yourpop()
and howtop
will act.