How can I modify my Stack Operation. (struct address problem)

29 views Asked by At

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?

1

There are 1 answers

0
lei zhou On

first, size should be an int value instead of DATA_TYPE. Then in push, you should use s->arr[++(s->size)]. But still, you need to make sure the stack and the arr are properly initialized/allocated. And I wonder how you want to implement your pop() and how top will act.