【问题标题】:Why isn't my scanf() not iterating for my for loop on my array?为什么我的 scanf() 没有为我的数组上的 for 循环迭代?
【发布时间】:2018-10-16 07:14:31
【问题描述】:

我正在使用 1D 和 2D 数组,但我的 scanf 没有为我的 1D 数组循环迭代。这是我当前的代码:

#include <stdio.h>

int main(void)
{
 int row, col, N, M;

 printf("This program counts occurrences of digits 0 through 9 in an NxM array.\n");
 printf("Enter the size of the array (Row Column): ");
 scanf("%d%d", &N, &M);

 int digits[N][M];

 for (row = 0; row < N; row++){
  printf("Enter row %d: ", row);
  scanf("%d", digits[row][0]);
 }
 return 0;
 }

【问题讨论】:

  • 你用的是什么编译器?

标签: c++ c arrays for-loop


【解决方案1】:

您在这一行中缺少一个&符号:

scanf("%d", digits[row][0]);

更正的代码:

scanf("%d", &digits[row][0]);

【讨论】:

    【解决方案2】:

    检查一下,

    scanf("%d", &digits[row][0]);
    

    您需要添加“&”。这将给出存储扫描变量的地址。单个整数存储在数组中也是如此。

    【讨论】:

      【解决方案3】:

      scanf 的最后一个参数必须是变量的地址,所以如果你想在这些变量中写一些东西,你需要用那里的地址而不是那里的值来传递它们。

      scanf("%d",&variable) --> 传递它的地址 scanf("%d", variable) --> 传递它的值

      【讨论】: