c - 阵列扫描两次?

标签 c arrays input scanf

#include <stdio.h>
int main()
{
   int array[1][1];
   for (int x = 0; x < 2;x++)
   {
     printf("x%d\n",x);
     for (int y = 0; y < 2;y++)
     {
       printf("y%d\n",y);
       scanf("%d\n",&array[x][y]);
     }
   }
   return 0;
}

我正在尝试扫描数组元素以启动矩阵计算器。该代码需要 5 个数字作为输入,而不是 4 个。在 x0 和 y0 之后,它需要两个数字,不知道为什么。我发布了输出。

x0
y0
1
1
y1
1
x1
y0
1
y1
1

最佳答案

这是一个工作版本:

#include <stdio.h>
int main()
{
   // Define a 2x2 array
   int array[2][2];

   for (int x = 0; x < 2;x++)
   {
     printf("x%d\n",x);
     for (int y = 0; y < 2;y++)
     {
       printf("y%d\n",y);
       scanf("%d", &array[x][y]);
     }
   }

   return 0;
}

其中 array[n] 的索引为 0..n-1,其中 if n=1 您只有一个有效索引。

关于c - 阵列扫描两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52156152/

相关文章:

C=C++;为什么 C 变量的值没有改变,因为它仍然是 0

python - 如何从C共享库返回无符号字符数组来调用python函数

javascript - 将子数组添加到 Javascript 中的对象中

javascript - jQuery 从输入中获取值并创建一个数组

c - C中的字符数组是否有最大返回长度

c - 如何对小型记录器使用可变参数?

python - python numpy数组操作+=线程安全吗?

c - 学习如何在使用 scanf 时正确引用结构体字段 (C)

css - 如何使提交输入和文本输入等距

python - (Python) 自动输入