I'm looking at this small program that creates a shell. parse() takes a character pointer line and an array of character pointers argv, saving the address of each word in argv.
void parse(char *line, char **argv) {
while (*line != '\0') { /* if not the end of line ....... */
while (*line == ' ' || *line == '\t' || *line == '\n')
*line++ = '\0'; /* replace white spaces with 0 */
*argv++ = line; /* save the argument position */
printf("%p\n",*argv);
while (*line != '\0' && *line != ' ' &&
*line != '\t' && *line != '\n')
line++; /* skip the argument until ... */
}
*argv = '\0'; /* mark the end of argument list */
}
What I don't understand is that argv is somehow back at the first word after the function exits. The main function calls:
parse(line, argv); /* parse the line */
if (strcmp(argv[0], "exit") == 0) /* is it an "exit"? */
exit(0);
How is argv[0] at the beginning of line when it was at the end of line right before?
*argv = '\0';,argvis a pointer to string not a string, do you mean*argv = NULL?'\0'is identical to0(i.e. anintwith value zero)NULLand 0 are interchangeable in this context.