I am declaring an array of strings by using:
char *commands[100];
I then proceed to enter a while loop that reads commands input from the user:
while(getcmd(buf, sizeof(buf), cmd_count) >= 0){
printf(2, "cmd_count: %d\n", cmd_count);
buf[strlen(buf)-1] = 0;
printf(2, "string buf: -%s-\n", buf);
commands[cmd_count] = buf;
printf(2, "commands: %s\n", commands[0]);
printf(2, "commands: %s\n", commands[1]);
printf(2, "commands: %s\n", commands[2]);
printf(2, "commands: %s\n", commands[3]);
cmd_count++;
}
Here is the output from two iterations:
0 EZ$ ls
cmd_count: 0
string buf: -ls-
commands: ls
commands: (null)
commands: (null)
commands: (null)
EZ$ echo hello
cmd_count: 1
string buf: -echo hello-
commands: echo hello
commands: echo hello
commands: (null)
commands: (null)
Even though cmd_count
was clearly 1 on the second iteration, it rewrote both the 0th and 1st position. What gives?