尝试在C中更改2D数组中的元素
我假设在函数make2Darray中实现,以使2D数组中的元素与行号相对应 打印和释放的代码的其他部分已经给出,所以不必担心。我只想触摸make2Darray函数。然而,在该函数中,也给出了分配部分。所以我要修改的唯一代码是我修改2D数组中元素的部分尝试在C中更改2D数组中的元素,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我假设在函数make2Darray中实现,以使2D数组中的元素与行号相对应 打印和释放的代码的其他部分已经给出,所以不必担心。我只想触摸make2Darray函数。然而,在该函数中,也给出了分配部分。所以我要修改的唯一代码是我修改2D数组中元素的部分 int** make2Darray(int width, int height) { int **a; int i = 0; int j = 0; /*allocate memory to store pointers for e
int** make2Darray(int width, int height) {
int **a;
int i = 0;
int j = 0;
/*allocate memory to store pointers for each row*/
a = (int **)calloc(height, sizeof(int *));
if(a != NULL) {
/* allocate memory to store data for each row*/
for(i = 0; i < height; i++) {
a[i] = (int *)calloc(width, sizeof(int));
if(a[i] == NULL) {
/* clean up */
free2Darray(a, height);
return NULL; /*aborting here*/
}
}
}
/* from this point down is the part I implemented, all code above was
given*/
if (height < 0 && width < 0) {
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
a[i][j] = j;
}
}
}
return a;
}
然而,我总是得到0,这是我得到代码时的默认设置
0 0 0
0 0 0
0 0 0
0 0 0
在代码中,
if
复选框似乎不合适
if(a != NULL) {
/* allocate memory to store data for each row?
for(i = 0; i < height; i++) {
a[i] = (int *)calloc(width, sizeof(int));
if(a[i] == NULL) {
/* clean up */
free2Darray(a, height);
return NULL; /*aborting here*/
}
if(a!=NULL){
/*是否分配内存来存储每行的数据?
对于(i=0;i
好吧,那没有意义。如果成功,a
应该不等于NULL
您应该检查a==NULL
,然后返回NULL;,而不是相反
也就是说:
看起来不太好。它很可能是错误的(在这个使用上下文中)并且适得其反if(height<0&&width<0)
- 对
的所有访问无效。您只为a[i][j]
s分配了空间,int*
s本身并不指向任何有效内存。您还需要为它们分配内存(例如,使用int*
宽度作为大小)
- 代码中有两个主要问题
1) 初始化2D数组的代码应位于if块内部
2)
if(高度<0&&width<0){
错误-您想要
而不是if(高度<0&&width<0){
这看起来不对你有/*分配内存来存储每一行的数据吗?
但结尾没有*/
。评论持续了几行,在评论过程中注释掉了关键代码。你真的打算这么做吗?哦,我的错。那只是一个打字错误,因为我在论坛上重新键入了代码。我回去了修复了它,谢谢。“如果(高度<0&&width<0)”。此时检查负值可能太晚了。您的程序可能已经崩溃了。即使这种情况以某种方式保持(奇迹般地或由于大量的愚蠢运气),下一行也是不可操作的,因为i
从不保持。您可能想说if(高度>0&&width>0)
(或者可能是=
)但在这一点上没有什么意义,因为for
循环实际上进行了必要的检查。您应该在第一次调用calloc
“我在论坛上重新键入了代码”之前检查它。永远不要这样做。复制并粘贴。如果您没有带代码,请打开联机编译器,在那里键入代码,验证它是否符合您所说的功能,然后从那里复制并粘贴。即使这样,数组也永远不会填充。请参阅上面的注释。@FedericoklezCulloca我正在扩展我的答案,谢谢您的注释。很抱歉吹毛求疵,但我不能hatif(height<0&&width<0)
不是毫无意义的。这是完全错误的。如果关键是填充数组,那么当这个条件存在时,它将永远不会被填充。@FedericoklezCulloca这就是我所说的反作用。如果这样可以消除混乱,我将删除“无意义”。
if(a != NULL) {
/* allocate memory to store data for each row?
for(i = 0; i < height; i++) {
a[i] = (int *)calloc(width, sizeof(int));
if(a[i] == NULL) {
/* clean up */
free2Darray(a, height);
return NULL; /*aborting here*/
}
int** make2Darray(int width, int height) {
int **a;
int i = 0;
int j = 0;
/*allocate memory to store pointers for each row*/
a = (int **)calloc(height, sizeof(int *));
if(a != NULL) {
/* allocate memory to store data for each row*/
for(i = 0; i < height; i++) {
a[i] = (int *)calloc(width, sizeof(int));
if(a[i] == NULL) {
/* clean up */
free2Darray(a, height);
return NULL; /*aborting here*/
}
}
// Moved inside the if(a != NULL) {
/* from this point down is the part I implemented, all code above was
given*/
if (height > 0 && width > 0) { // Corrected this part
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
a[i][j] = j;
}
}
}
}
return a;
}
if (height <= 0 || width <= 0) return NULL;
int** make2Darray(int rows, int columns) {
int **a;
int i = 0;
int j = 0;
if (rows <= 0 || columns <= 0) return NULL;
a = calloc(rows, sizeof(int*));
if(a == NULL) return NULL;
/* allocate memory to store data for each row*/
for(i = 0; i < rows; i++) {
a[i] = calloc(columns, sizeof(int));
if(a[i] == NULL) {
/* clean up */
free2Darray(a, rows);
return NULL; /*aborting here*/
}
}
/* from this point down is the part I implemented, all code above was
given*/
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
a[i][j] = j;
}
}
return a;
}