【发布时间】:2015-02-26 20:24:27
【问题描述】:
我的任务是创建函数:
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
【问题讨论】:
-
您的代码中没有 36 行 ..
-
我认为当代码出现在项目符号点之后时存在一些格式问题。
-
while (inp[i] != '/0' ) {-->while (inp[i] != '\0' ) {或者只是:while (inp[i]) {