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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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 数组[0]在进入“for”循环时正在更改,无法找出原因_C_Arrays_Algorithm_For Loop_Variable Length Array - Fatal编程技术网

C 数组[0]在进入“for”循环时正在更改,无法找出原因

C 数组[0]在进入“for”循环时正在更改,无法找出原因,c,arrays,algorithm,for-loop,variable-length-array,C,Arrays,Algorithm,For Loop,Variable Length Array,我正在对HackerRank进行挑战,我找到了方法,但有一个小错误,我无法找出。如有需要,请提供更多信息 基本上我只对arr[0]有一个问题。它将arr[0]存储为'aba',然后一旦它到达第一个for循环,它就变为'ab'。为什么? 输入: 4 aba baba aba xzxb 3 aba xzxb ab 代码: 在初始化q之前,您将测试和计数器声明为大小为q的数组。在扫描%d之后移动到那里,&q;。同时移动计数器的初始化循环: 试试这个,在声明和初始化后使用varialble谢谢!!!唉

我正在对HackerRank进行挑战,我找到了方法,但有一个小错误,我无法找出。如有需要,请提供更多信息

基本上我只对arr[0]有一个问题。它将arr[0]存储为'aba',然后一旦它到达第一个for循环,它就变为'ab'。为什么?

输入:

4
aba
baba
aba
xzxb
3
aba
xzxb
ab
代码:

在初始化q之前,您将测试和计数器声明为大小为q的数组。在扫描%d之后移动到那里,&q;。同时移动计数器的初始化循环:


试试这个,在声明和初始化后使用varialble

谢谢!!!唉,真不敢相信我有时做的傻事。将在最短接受时间结束时接受此答案。在给出q之前声明的char*测试[q]!为什么要在函数顶部声明所有变量?这在C89中是必要的,但是。。。差不多30年了。这被认为是很长一段时间的坏习惯。请记住这一点,谢谢!至于答案,我是~3周大的程序员
int main() {
  int i, j;
  int n;
  int q;
  scanf("%d", &n);
  char* arr[n];
  char* test[q];
  char* s;
  int counter[q];

  for (i = 0; i < q; i++) {
    counter[i] = 0;
  }

  for (i = 0; i < n; i++) {
    arr[i] = malloc(20);
    scanf("%s", arr[i]);
  }

  scanf("%d", &q);

  for (i = 0; i < q; i++) {
    test[i] = malloc(20);
    scanf("%s", test[i]);
  }

  for (i = 0; i < n; i++) {

    for (j = 0; j < q; j++) {

      if (strcmp(arr[i], test[j]) == 0) {

        counter[j]++;
      } else {
      }
    }
  }
  for (i = 0; i < q; i++) {
    printf("%d\n", counter[i]);
  }
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int i, j;
  int n;
  int q;
  scanf("%d", &n);
  char* arr[n];
  char* s;

  for(i=0; i<n; i++) {
    arr[i]= malloc(20);
    scanf("%s",arr[i]);
  }

  scanf("%d", &q);
  int counter[q];
  char* test[q]; 

  for(i=0; i<q; i++) {
    counter[i] = 0;
  }
  for(i=0; i<q; i++) {
    test[i]= malloc(20);
    scanf("%s",test[i]);
  }

  for(i=0; i<n; i++) {
    for(j=0; j<q; j++) {
      if (strcmp(arr[i],test[j]) == 0) {
        counter[j]++;
      }
    }
  }
  for(i=0; i<q; i++) {
    printf("%d\n", counter[i]);
  }
  return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
  int i, j;
  int n;
  int q;
  scanf("%d", &n);
  char* arr[n];


  for (i = 0; i < n; i++) {
    arr[i] = malloc(20);
    scanf("%s", arr[i]);
  }

  scanf("%d", &q);

  char* test[q];
  char* s;
  int counter[q];


  for (i = 0; i < q; i++) {
    counter[i] = 0;
  }
  for (i = 0; i < q; i++) {
    test[i] = malloc(20);
    scanf("%s", test[i]);
  }

  for (i = 0; i < n; i++) {

    for (j = 0; j < q; j++) {

      if (strcmp(arr[i], test[j]) == 0) {

        counter[j]++;
      } else {
      }
    }
  }
  for (i = 0; i < q; i++) {
    printf("%d\n", counter[i]);
  }
  return 0;
}