【发布时间】:2014-05-29 08:57:28
【问题描述】:
我正在填充一个 C 程序来将 2 个输入向量相乘。代码如下:
/**
* Function name: parseArguments
* Description:
* Determine what options or arguments user used
* Command-line options:
* [-h] : Show help information
* [-n] num: Determine number of threads
* file1 : Choose first file
* file2 : Choose second file
* Return value:
* 0: If parsing successful
* exit: If error
**/
static int parseArguments(int argc, char **argv, int* nthreads, char* file1, char* file2)
{
int opt;
int help=0;
extern int optind;
extern char * optarg; // (global variable) command-line options
while ((opt = getopt(argc, argv, "hn:")) != EOF)
{
switch (opt) {
//Parse option -h, -n
case 'n':
*nthreads = atoi(optarg);
break;
case 'h':
Usage();
exit(1);
break;
default:
fprintf(stderr, "Try 'mulvector -h' for more information\n");
exit(1);
}
// parse file1 & file2 arguments
// THIS IS WHAT I'M ASKING
if (optind < argc)
{
file1 = &argv[optind];
optind++;
}
if (optind < argc)
file2 = &argv[optind];
}
return 0;
}
问题是,在我调用这个函数(在 main() 函数中)然后退出这个函数(继续 main() 函数)之后,2 个变量 file1 和 file2 在执行 parseArguments 之前仍然保持它们的旧值功能。我正在尝试解决这个问题,但我没有得到任何结果......
希望大家帮忙,非常感谢!
注意: file1 和 file2 的类型是 char file1[1024] 所以我不能使用 char** 作为 parseArguments 函数的参数!
【问题讨论】:
标签: c pointers command-line