The getword function I wrote takes in buffered input based off of getchar(). It delimits also by metacharacters. After every call, it moves its pointer to the next word in the inputted buffer string and places the most recent one in the char pointer it takes as a parameter, in this case it is the character array called buffer.
Objective: Using the getword function I wrote, I need to store each returned word/token into consecutive indices of the char*args[] I have set up.
My problem is that I cannot properly load these individual tokens into separate indices of my char *args[]. Instead, it seems to store the most recent word into all indices of args after every pass of the for loop. For example, if I type in:
Hello world, this is a test.
It will store the words into args in this way:
- pass1:
args[0]: Hello - pass2:
args[0]: world,args[1]: world, - pass3:
args[0]: thisargs[1]: thisargs[2]: this
Code:
int main (int argc, char *argv[]) {
char buffer[MAXBUF], *args[MAXITEM];
int c = 1;
for (i = 0; c > 0; i++) {
c = getword(buffer);
args[i] = buffer;
int g = 0;
while(args[g] != NULL) {
printf("args[%d]: %s\n", g, args[g]);
g++;
}
}
}
argsin a debugger, and you'll see each,args[0],args[1], etc.. are storing the same address.