Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中编译parse_command()函数时出错_C_Arrays_Pointers - Fatal编程技术网

在C中编译parse_command()函数时出错

在C中编译parse_command()函数时出错,c,arrays,pointers,C,Arrays,Pointers,我的任务是创建函数: int parse_command(char *inp, int *argc, char *argv[]); 该职能应: 将字符串inp拆分为单词,并返回单词数 两个单词之间用一个或多个空格隔开 此外,argc应设置为字数 argv[0]应该指向第一个单词,argv[1]指向第二个单词,依此类推。注意:您应该能够使用argv指针打印每个单词 这是我的密码: int parse_command (char *inp, // original string

我的任务是创建函数:

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

如果将argc声明为int*,那么第“36”行应该是这样的:

argv[(*argc)] = argv+inp[i];

通过这种方式,您可以取消对整数的引用,得到一个整数。

代码中没有36行。

当代码出现在项目符号之后时,我认为会出现一些格式化问题。
while(inp[i]!='/0'){
-->
while(inp[i]!='\0'){
或只是:
while(inp[i]){
OP实际上注释掉了该行,但是它是
argv[argc]=argv+inp[i]
我假设它被注释掉了,因为它导致了错误(从注释中)。argv[argc]将成为自[]应该是介于两者之间的整数。相反,由于argc声明为int*,因此OP正在传递一个指针。通过doing(*argc)取消对它的引用将为它们提供数组下标所需的int。哦,不,你是对的(因此是upvote),我只是告诉你OP代码中的实际行是什么,这样你就可以把它编辑到你的答案中。