0

I am writing a series of stack manipulation functions in C, and I am having an issue with pop. The tester file requires a return of ItemT where ItemT is just the define for char *.

ItemT popStack(StackP stackPtr) {
  if (isEmptyStack(stackPtr)) {
    fprintf(stderr, "\nERROR: Empty stack...\n");
    return NOTHING;
  }
  else {
    ItemT item = *((stackPtr->items));
    (stackPtr->top)--;
    (stackPtr->count)--;
    --(stackPtr->items);
    return item;
  }
}

items of (stackPtr->items) is a ItemT *, or a char **

in the line ItemT item = *((stackPtr->items)); I am trying to get the char * that stackPtr->items is pointing to and set it to the variable item and then return item.

My bug is the return, when the tester program calls pop and tries to display the value returned by popStack() what is displayed is this:

enter image description here

The stack is full of char * and peek works just fine. It is just pop here and seems to not work.

I just assume that that is bad data or random values stored in empty memory addresses. Thus, I have no idea what I am doing wrong. Any way someone can explain to me what is wrong here? and is there an issue with my method of decrementing the items in the struct?

EDIT 1:

the call to popStack() looks something like:

printf("%s\n", popStack(stackP)); where stackP is the pointer to the struct that contains all the info. You can ignore top and count because they work effectively. The --(stackPtr->items) I have just learned is absolutely ineffective.

6
  • Post more code please, and don't do this *((stackPtr->items)) it's not very easy to follow, instead just stackPtr->items[0]. Commented Jan 23, 2015 at 2:46
  • I literally just figured that out. Now I am working on trying to get the logic right. I've been incrementing the pointer. Commented Jan 23, 2015 at 2:49
  • Also, isn't that what top is for, so shouldn't it be item = stackPtr->items[stackPtr->top]; and don't alter the pointer, or else how are you going to free it? Commented Jan 23, 2015 at 2:52
  • @iharob I added a little more, i hope that helps. Thanks for the bracket info im working with that now and seeing if i can't get rid of that bad data. Commented Jan 23, 2015 at 2:54
  • i use count for something else, just a something i added for ease that i won't go into. The way i see it I could use top or count and it would work the same as long as i use the logic correctly. Commented Jan 23, 2015 at 2:56

1 Answer 1

1

I think you should change this

ItemT item = *((stackPtr->items));

to

ItemT item = stackPtr->items[stackPtr->top];

and remove this

--(stackPtr->items);
Sign up to request clarification or add additional context in comments.

1 Comment

did that, but now i'm working at a segmentation fault because of bad logic, alot of my program needs a little fine tuning because of adding the change you suggested, but it is putting me in the right direction after five hours. thanks @iharob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.