如何扫描到 C 中多维数组的一维?
How to scanf to just one dimension of a multidimensional array in C?
假设我有一些这样的 C 代码,它声明了一个小的多维数组:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int mat[5][2] = {
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0}
}
do scanf("%i", &mat[][]);
while (getchar() != '\n');
return 0;
}
并且我想将 do scanf("%i", &mat[][]);
及其下面的行更改为允许我将用户提供的整数读取到此 5×2 多维数组的第二列中的内容。
我正在尝试找到最简单的解决方案,不考虑软件安全性,希望不会不必要地调用库。
好吧,如果我正确理解你对 "column" 的定义,你需要为第二个索引提供固定值,并在循环时询问用户该值第一个索引。
一些伪代码可能看起来像
for (i = 0; i < 5; i++)
scanf("%d", &mat[i][1]);
你想要这样的东西。
int i=0;
do{
scanf("%d", &mat[i][1]);
i++;
}while (getchar() != '\n');
假设我有一些这样的 C 代码,它声明了一个小的多维数组:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int mat[5][2] = {
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0}
}
do scanf("%i", &mat[][]);
while (getchar() != '\n');
return 0;
}
并且我想将 do scanf("%i", &mat[][]);
及其下面的行更改为允许我将用户提供的整数读取到此 5×2 多维数组的第二列中的内容。
我正在尝试找到最简单的解决方案,不考虑软件安全性,希望不会不必要地调用库。
好吧,如果我正确理解你对 "column" 的定义,你需要为第二个索引提供固定值,并在循环时询问用户该值第一个索引。
一些伪代码可能看起来像
for (i = 0; i < 5; i++)
scanf("%d", &mat[i][1]);
你想要这样的东西。
int i=0;
do{
scanf("%d", &mat[i][1]);
i++;
}while (getchar() != '\n');