【发布时间】:2015-02-18 06:20:38
【问题描述】:
我需要转换命令行中给出的参数,例如:$ myprogram hello world
并且单词需要用大写字母打印。除了访问双指针数组以使用 toupper() 进行更改之外,我可以做任何事情
static char **duplicateArgs(int argc, char **argv)
{
char **copy = malloc(argc * sizeof (*argv));
if(copy == NULL){
perror("malloc returned NULL");
exit(1);
}
int i;
for(i = 0; i<argc; i++){
copy[i] = argv[i];
}
char **temp;
temp = ©[1];
*temp = toupper(copy[1]);
return copy;
}
【问题讨论】:
-
请正确缩进您的代码。
-
看来你用的是vim,代码缩进的快捷方式是
GG=g -
char **temp; temp = &copy[1]; *temp = toupper(copy[1]);- 你目前认为这是做什么的? -
另外,请注意,当您复制
argv本身(它是指向字符串指针数组的指针)时,您并没有复制单个字符串。 -
您需要“转换命令行给出的参数”还是“转换命令行给出的参数”?正如我看到你的例子与
hello word和argc参数duplicateArgs我想你想处理所有的论点,但接受的答案让我怀疑