我的任务是创建函数:
int parse_command(char *inp, int *argc, char *argv[]);
该功能应该:
inp
拆分为单词,并返回单词数。 argc
应设置为单词数argv[0]
应该指向第一个单词argv[1]
到第二个单词,依此类推。注意:您应该能够使用argv
指针这是我的代码:
int parse_command (char *inp, // original string
int *argc, // number of words
char *argv[]) { // array of words
// Split the string inp into words (Two words are separated by one or more blank spaces)
int i = 0;
int j = 0;
int a;
while (inp[i] != '/0' ) {
while (inp[i] == ' ') {
if (inp[i + 1] != ' ') {
inp[i + 1] = '/0'; // end last word (add a /0 to the last word)
printf("here");
(*argc)++; // add new word to array of words
argv[j++]; // argv(0) = i or j?
}
i++; // it's confusing here
}
// The line I commented out below is where I get this error:
// array subscript is not an integer
// argv[argc]= argv+inp[i]; // add letter to current word
i++;
}
inp[i + 1] = '/0'; // end last word
return j; // return the number of words (addition, argc should be set to the number of words)
}
当我尝试在当前单词中添加一个字母时出现错误。错误是:
array subscript is not an integer
答案 0 :(得分:1)
如果您已将argc声明为int *,那么“36”行应该说:
argv[(*argc)] = argv+inp[i];
这样你就可以取消引用整数的指针,并得到一个整数。