gpt4 book ai didi

c - C中的动态内存分配二维数组

转载 作者:行者123 更新时间:2023-12-02 07:04:33 25 4
gpt4 key购买 nike

<分区>

int **twoDary = (int**) (malloc(rows * sizeof(int *)));
int **twoDaryStart = twoDary;
int *currentrow;

for ( i = 0; i < rows; i++ ){ // Originally: for (i = 0; i < columns; i++)
*(twoDary + i) = (malloc(columns * sizeof(int)));
}

for (j = 0; j < rows; j++) {
currentrow = *(twoDary + j);
for ( i = 0; i < columns; i++ ) {
*(currentrow + i) = i;
printf("%d\n", *(currentrow+i));
}
}

我正在尝试创建一个动态二维数组。然后我试图将 i ,当前 i (在内部 for 循环中)分配给每一行中的每个元素。所以我的输出应该是数字 0 - 列打印的行时间。

如果我的行和列不相同,即 5 行 10 列,我会不断收到段错误。谁能从这段代码中看出为什么会发生这种情况?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com