Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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字符数组的意外输入_C_Arrays_Char_Undefined Behavior - Fatal编程技术网

C字符数组的意外输入

C字符数组的意外输入,c,arrays,char,undefined-behavior,C,Arrays,Char,Undefined Behavior,我和一个小组一起研究谷歌代码阻塞练习问题(你可以阅读)。我们的守则如下: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> main(){ int c; int n = 0; int l = 0; int d = 0; int caseCount = 0; int i = 0; int j = 0; //A boolean value used in det

我和一个小组一起研究谷歌代码阻塞练习问题(你可以阅读)。我们的守则如下:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

main(){
  int c;
  int n = 0;
  int l = 0;
  int d = 0;
  int caseCount = 0;
  int i = 0;
  int j = 0;
  //A boolean value used in determining parenthesees.
  bool letBool = false;
  //A boolean value used in determining something;
  bool wordBool = false;
  //Temporary space for test characters
  char test[1000];
  //Gets word length
  while((c=getchar())!=' ' && c!= '\n'){
    l = (l * 10) + c - '0';
    //printf("%d\n", l);
  }
  //Gets number of valid words.
  while((c=getchar())!=' ' && c!= '\n'){
    d = (d * 10) + c - '0';
    //printf("%d\n", d);
  }
  //Gets number of test cases.
  while((c=getchar())!= '\n'){
    n = (n * 10) + c - '0';
    //printf("%d\n", n);
  }
  //Array of all valid words.
  char dict[d][l];
  c=getchar();
  //While we still have words to read in.
  while(i < d){
    //If not new line
    if(c!='\n'){
      //Then read character
      dict[i][j] = c;
    }
    else{
      i++;
      j=0;
    }
    c=getchar();
    j++;
  }
  i = 0;
  j = 0;
  while(i < n){
    j = 0;
    while((c=getchar())!='\n' && c!=EOF){
      putchar(c);
      test[j] = c;
      j++;
    }
    putchar('\n');
    test[j+1] = '\0';
    printf("%s\n", test);
    int word = 0;
    //Going through valid words
    while(word < d){
      wordBool=true;
      j = 0;
      int letter = 0;
      //Going through valid letters
      while(letter < l){
        letBool=false;
        if(test[j] == '('){
          while(test[j++]!=')'){
            if(dict[word][letter]==test[j]){
              letBool=true;
              //printf("%s%d%s%d\n" "letBool is true at word: ", word, "letter: ", letter);
            }
          }
        }
        else{
          if(test[j]==dict[word][letter]){
            letBool=true;
            //printf("%s%d%s%d\n" "letBool is true at word: ", word, "letter: ", letter);
          }
        }
        j++;
        letter++;
        if(!letBool){
          wordBool=false;
          //printf("%s%d%s%d\n" "wordBool is false at word: ", word, "letter: ", letter);
        }
      }
      if(wordBool){
        caseCount++;
      }
      word++;
    }
    printf("%s%d%s%d\n", "Case #", i+1, ": ", caseCount);
    i++;
    j=0;
    caseCount=0;
  }
}
当我们给它这个输入时:

3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc
我们得到

ab)(bc)(ca)
ab)(bc)(ca)
7
Case #1: 0
abc
abc
b
Case #2: 1
(abc)(abc)(abc)
(abc)(abc)(abc)
Case #3: 0
(zyx)bc
(zyx)bcb
Case #4: 0

(
Case #5: 0
因此,我们无法理解为什么会打印出像7和b这样的随机字符。

更改此选项:

test[j+1] = '\0';
为此:

test[j] = '\0';
原因是在while循环结束时,在满足终止条件之前,您已经增加了
j
的值:

j = 0;
while((c=getchar())!='\n' && c!=EOF){
  putchar(c);
  test[j] = c;
  j++;    <------
}
putchar('\n');
test[j] = '\0';

此外,使用:

int main(void) {
  ...
  return 0;
}
有关更多信息,请阅读以下内容:

更改此内容:

test[j+1] = '\0';
为此:

test[j] = '\0';
原因是在while循环结束时,在满足终止条件之前,您已经增加了
j
的值:

j = 0;
while((c=getchar())!='\n' && c!=EOF){
  putchar(c);
  test[j] = c;
  j++;    <------
}
putchar('\n');
test[j] = '\0';

此外,使用:

int main(void) {
  ...
  return 0;
}

更多信息,请阅读:

这里有一个bug:
test[j+1]='\0'应该是
test[j]='\0'
,因为
j
已经增加到上一个字符索引之后。非常感谢您的帮助!那是一个竞赛场地。你有能力自己解决这个问题。你甚至可以找到答案,总是有“作弊”网站。我不想作弊,这只是练习。我的小组无法理解,我们只是想知道出了什么问题。@TomKarzes OP提到了评论部分,我现在看到你也发现了这个错误,在发布答案之前不知道!这里有一个bug:
test[j+1]='\0'应该是
test[j]='\0'
,因为
j
已经增加到上一个字符索引之后。非常感谢您的帮助!那是一个竞赛场地。你有能力自己解决这个问题。你甚至可以找到答案,总是有“作弊”网站。我不想作弊,这只是练习。我的小组无法理解,我们只是想知道出了什么问题。@TomKarzes OP提到了评论部分,我现在看到你也发现了这个错误,在发布答案之前不知道!谢谢我现在正在学习c语言,所以我不知道主要功能应该有这些标准!对不起,如果问题问得不好,就像几分钟前一位高年级同学告诉我的那样!这就是我真正需要的,因为问题的其余部分肯定在我的能力范围内,我可以找出为什么我们没有得到正确数量的病例@MegaZeroX现在您已经学会了。:)确保检查我的最新答案(刚才做了)!好的,祝你好运!谢谢我现在正在学习c语言,所以我不知道主要功能应该有这些标准!对不起,如果问题问得不好,就像几分钟前一位高年级同学告诉我的那样!这就是我真正需要的,因为问题的其余部分肯定在我的能力范围内,我可以找出为什么我们没有得到正确数量的病例@MegaZeroX现在您已经学会了。:)确保检查我的最新答案(刚才做了)!好的,祝你好运!