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
stack[stack_pointer] = strdup(command_line);