Hi everyone,
So I'm working on a stacks project and i every time i run the following function, the program runs fine until the end but it only crashes once i press enter to exit out of the command screen. I'm thinking it may be some sort of memory access error.. but i can't figure out exactly where it goes wrong. S->data is a pointer to an array of stackItems and itemPtr is just a pointer to a stackItem.
Please help! and thank you!!
Code:
int stack_peek( stackItem *itemPtr, Stack S )
/*This function takes as arguments a Stack S and a pointer to a stackItem
itemPtr. If S is not empty, the top item is copied into the space pointed to by
itemPtr. It returns 1 of S is not empty, 0 if it is empty, and -1 of any of
its arguments is NULL.*/
{
if (S){ // check if S exists
if (S->count==0) { // check if S is empty
return 0;
}
else { // if Stack is not empty
*itemPtr = S->data[S->top]; // copy value of top item to itemPtr
return 1;
}
}
else { // if S is NULL, return -1
return -1;
}
}