Originally Posted by
inu11byte
Why could I pass an the argument 'argv[0]' into my original program?
argv[0] has type `char *' and your function accepts an argument of type `char []'. In C, these two are equivalent.
However, the following may work but is probably not correct:
Code:
#include <stdio.h>
void pause(){printf("Press enter to continue...\n");getchar();}
void tokenize_arg(char a[]);
int main(int argc, char *argv[])
{
tokenize_arg(argv[0]);
pause();
}
void tokenize_arg(char a[])
{
char delims[] = "/\\";
char *token = strtok(a, delims);
while (token != NULL) {
printf("Got a token \"%s\"\n", token);
token = strtok(NULL, delims);
}
}
If I run this on my Windows machine, I get the correct output (drive name, directory, directory, then my program name). However, argv[] is not in general a modifiable array. So you should better make a copy before using strtok():
Code:
void tokenize_arg(char a_readonly[])
{
char a[strlen(a_readonly)+1];
strcpy(a, a_readonly);
char delims[] = "/\\";
char *token = strtok(a, delims);
while (token != NULL) {
printf("Got a token \"%s\"\n", token);
token = strtok(NULL, delims);
}
}