0

Could someone please explain why this is not working correctly? I get stack{0 through 4] to be the same as what ever is in stack[4]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int stack_pointer = 0;
char *stack[30]; //MAX NUMBER OF VALUES IN STACK IS 30

int main(int argc, char** argv) {

    char command_line[256];

    while(stack_pointer < 5) {  //Just store 5 inputs
        printf("repl> ");
        scanf("%s",command_line);
        stack[stack_pointer] = command_line;  
        stack_pointer++;
    }   

    int i = 0;
    for(i = 0; i<5; i++) {
        printf("stack[%d] =  %s\n", i, stack[i]);
    }
}

EX)

repl> 1
repl> 2
repl> 3
repl> 4
repl> 5
stack[0] = 5
stack[1] = 5
stack[2] = 5
stack[3] = 5
stack[4] = 5

2
  • stack[stack_pointer] = strdup(command_line); Commented Feb 18, 2014 at 16:26
  • Pointer is pointing to the same place(to command_line). Commented Feb 18, 2014 at 16:28

3 Answers 3

3

You are storing the char pointer command_line in each stack position and overwriting the contents of the command_line buffer for each input. So each stack position points to the same buffer and its contents will be the last input written to it.

You need to make a copy of the buffer each time and store a pointer to the copy in each stack position. For example:

stack[stack_pointer] = strdup(command_line);

Note that at some point, in the real program, you will need to free the memory allocated by strdup. When you've finished using the contents of the stack:

while (stack_pointer >= 0) free(stack[--stack_pointer]);
Sign up to request clarification or add additional context in comments.

3 Comments

I would add a comment of freeing the memory strdup creates
@halfbit: good point - updated.
isedev: Thank you so much! It makes sense now!
1

your stack[pointer] points to the beginning of command_line

so all elements of the stack array are pointing to the same position, that is holding the last result

you are overwriting this memory position with each loop.

Comments

0

edit: oops, duplicated my answer, sry

your stack[pointer] points to the beginning of command_line

so all elements of the stack array are pointing to the same position, that is holding the last result

you are overwriting this memory position with each loop.

edit: other solution not the best, but showing pointers:

int stack_pointer = 0;
char *stack[30]; //MAX NUMBER OF VALUES IN STACK IS 30

int main(int argc, char** argv) {

   char command_line[32000];  // not save! just for ex. here never do that in real life
   char *p=command_line;      // p points to start of ...

   while(stack_pointer < 5) {  //Just store 5 inputs
      printf("repl> ");
      scanf("%s",p);          // p points to start of (next) command line position
      stack[stack_pointer] = p;  
      stack_pointer++;

      p+=strlen(p)+1;           // move the 'p' pointer after the last string (and its '0x00')
   } 
}  

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.