c - c中二维数组中的随机值

标签 c arrays matrix

好吧,我有一个 int 的二维数组,它是用这个创建的:

int **matrix;
matrix = malloc(n * sizeof(int*));
for (int i = 0; i < n ; i++) {
    matrix[i] = malloc(sizeof(int));
}

现在我将每个字段都设置为 0。 下一步是用值填充它。因此,我从已打开的文件中读取值。

while (fscanf(f2, "%s -> %[^;\n]%*c", start, ziel) == 2) {
    intomatrix(start, ziel, &graph);
}

intomatrix 只是改变数据并将其放入矩阵中使用

matrix[start][ziel] = 1;

现在当我手动设置时(不使用 while 循环)

matrix[2][0] = 1

并打印矩阵。不仅 (2,0) 而且 (0,4) 都设置为 1。 现在应该有使用 matrix[0][4] 的方式。

这也发生在其他领域。 (2,0) 也设置 (1,4)。

最佳答案

应该这样:

matrix[i] = malloc(sizeof(int));

是一个整数数组还是一个整数?如果不是,你有多行,只有一列。

关于c - c中二维数组中的随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30512271/

相关文章:

arrays - 如何删除二维数组中的相似数组?

c - stm32f4板上的usb otg与i2c冲突

c - 使用主应用程序窗口使拥有的窗口最小化

c - Win32开发。标准

python - 在 Cython 中返回一个结构数组

javascript - JavaScript 中对象的 Array.prototype.map() 等价物是什么

matlab - 按某些列值过滤矩阵

c++ - 在 Matrix4::Perspective 中反转 Z 轴

matlab - 如何最好地构造一个矩阵,其元素恰好是它们的索引或 Matlab 中索引的函数?

c++ - 更改全局变量名称