Hello all... again,

I'm trying to write a function that will "simulate" the stack. Here's what I have so far...

int i = 0;
int arraySize;
char * stack[100];

void stackPush(char * pushed)
{
    if(i == 100)
    {
        cout << "ERROR: Stack overflow!";
    }
    else
    {
        stack[i] = pushed;
        for(int x = 0; x <= i; x++)
        {
            cout << stack[x] << endl;
        }
        i++;
    }
}

The only problem is, when you "push" something onto this stack, it writes it to THE ENTIRE STACK. I've tried making it only write to one element (the "stack" part), but it still writes to the entire thing. I know this is a trivial question, but my GoogleFu and mind are failing me right now. Help?

What are you pushing on the stack? If you are just modifying what the char-pointer points to, then yeah, it will change all the stack entries. Try with std::string instead of char-pointers. As in:

int i = 0;
int arraySize;
std::string stack[100];

void stackPush(const std::string& pushed)
{
    if(i == 100)
    {
        cout << "ERROR: Stack overflow!";
    }
    else
    {
        stack[i] = pushed;
        for(int x = 0; x <= i; x++)
        {
            cout << stack[x] << endl;
        }
        i++;
    }
}

What are you pushing on the stack? If you are just modifying what the char-pointer points to, then yeah, it will change all the stack entries. Try with std::string instead of char-pointers. As in:

Oh, sorry, I'm trying to push a char array input into it (which is why I'm not using strings). Here's the code for that:

cout << endl;
cout << "$: ";
char uInput[256]; // Only 256 characters allowed.
char * token = NULL; // Where our tokens will be stored
const char * uStackPush = "stackPush";
cin.getline(uInput, 256); 
token = strtok(uInput, " "); 
while(token != NULL) 
    {
        if(!strcmp(uStackPush, token))
        {
            token = strtok(NULL, " ");
            stackPush(token);
            token = NULL;
            break;
        }

Your problem isn't with your stack code. I copied it and wrote a simple main() that pushed three constant strings onto it, and it behaved as expected. Instead, understand what strtok() is doing: it's internally maintaining a static pointer to the next token in the string, and returning that. Since you're pushing the returned pointer onto your stack, you're actually putting the same pointer on your stack over and over, while the next call to strtok() changes what all of those identical pointers now point to!

So, if you're determined to stick to char *, you could do stackPush(strdup(token)); , where strdup() is making a copy of the token for you (and don't forget then that since you're effectively calling malloc(), you're also responsible for calling free() on each string as you remove it from the stack, once you're done with it). Or you could take mike's advice and make a string-type via std::string str (token); and push that on a stack of strings, and the memory will be allocated and released for you by the string class.

This article has been dead for over six months. Start a new discussion instead.