我有以下代码:
void parse(char *commandLine) {
int rc = 0;
int argc = 0;
char *cmdLine;
char *argv[MAX_ARGS];
filename = NULL;
stdoutFilename = NULL;
stderrFilename = NULL;
cmdLine = strdup(commandLine);
char *param = strtok(cmdLine, " ");
while (param && argc < MAX_ARGS) {
argv[argc++] = param;
param = strtok(NULL, " ");
printf("%s\n", argv[argc-1]);
}
free(cmdLine);
scanOptions(argc, argv);
printf("Filename %s\n", filename);
...
和
void scanOptions(int argc, char *argv[]) {
int c ;
while ((c = getopt (argc, argv, "Df:e:o:")) != -1) {
switch (c) {
case 'D': __debug = 1; break;
case 'f': filename = strdup(optarg); break;
case 'o': stdoutFilename = strdup(optarg); break;
case 'e': stderrFilename = strdup(optarg); break;
default: fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
}
}
}
文件名,stdoutFilename
并且stderrFilename
是全局变量。如果我将解析方法称为:
parse("-ftest/testfile.txt") the variable filename is not set and the call to
printf("Filename %s\n", filename); prints "Filename (null)".
那有什么问题?