Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中检查动态分配2d数组的元素是否为NULL_C_For Loop_Multidimensional Array_Data Structures_Null - Fatal编程技术网

如何在C中检查动态分配2d数组的元素是否为NULL

如何在C中检查动态分配2d数组的元素是否为NULL,c,for-loop,multidimensional-array,data-structures,null,C,For Loop,Multidimensional Array,Data Structures,Null,我有一个指向结构的动态分配2d数组。我试图用数字0填充数组的空值。没有错误或警告,只是打印了一些随机值 这是代码的简化版本: #include <stdio.h> #include <stdlib.h> int rows = 2, cols=3 ; struct Data { int trail; }; struct Data** WritingData() { //initializing 2d array struct Data **ar

我有一个指向结构的动态分配2d数组。我试图用数字0填充数组的空值。没有错误或警告,只是打印了一些随机值

这是代码的简化版本:

#include <stdio.h>
#include <stdlib.h>

int rows = 2, cols=3 ;

struct Data
{
    int trail;
};

struct Data** WritingData()
{
    //initializing 2d array
    struct Data **arr = (struct Data**)malloc(rows * sizeof(struct Data*));
    for (int i=0; i<cols; i++)
         arr[i] = (struct Data*)malloc(cols * sizeof(struct Data));

    //giving some values as an example
    (*(arr+0)+0)->trail = 1;
    (*(arr+1)+0)->trail = 1;
    (*(arr+0)+1)->trail = 1;

    //checking if value is NULL to replace with 0
    for (int i = 0; i < rows ; i++) {
        for (int j = 0; j < cols; j++){
              if (!((*(arr+i)+j)->trail))
             (*(arr+i)+j)->trail = 0;
              else continue;
            }
        }

    return(arr);
}

int main()
{
    struct Data **arr = WritingData();

    //printing result
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
        {
        printf(" %d ", (*(arr+i)+j)->trail);
        }
    printf("\n");
    }

    free(arr);
}
#包括
#包括
int行=2,cols=3;
结构数据
{
int trail;
};
结构数据**写入数据()
{
//初始化二维数组
结构数据**arr=(结构数据**)malloc(行*sizeof(结构数据*);
对于(inti=0;itrail=1;
(*(arr+1)+0)->trail=1;
(*(arr+0)+1)->trail=1;
//正在检查值是否为NULL以替换为0
对于(int i=0;itrail))
(*(arr+i)+j)->trail=0;
否则继续;
}
}
返回(arr);
}
int main()
{
结构数据**arr=WritingData();
//打印结果
对于(int i=0;itrail);
}
printf(“\n”);
}
免费(arr);
}
对于该循环的启动程序

for (int i=0; i<cols; i++)
              ^^^^^^^
     arr[i] = (struct Data*)malloc(cols * sizeof(struct Data));
for (int i=0; i<rows; i++)
     arr[i] = (struct Data*)calloc(cols, sizeof( struct Data ));
没有意义,因为未初始化的对象具有不确定的值,并且函数malloc不初始化分配的内存

例如,您可以在此循环中使用
calloc
而不是
malloc

for (int i=0; i<cols; i++)
              ^^^^^^^
     arr[i] = (struct Data*)malloc(cols * sizeof(struct Data));
for (int i=0; i<rows; i++)
     arr[i] = (struct Data*)calloc(cols, sizeof( struct Data ));

您还需要在一个循环中释放所有分配的子数组。

对于“int”数据类型(如
trail
成员),
NULL
与零相同。因此,您的测试/集确实在做:
如果(x==0)x=0;
。现在看到问题了吗?此外,除非显式初始化,否则数据成员将具有未定义的值(看起来是随机的)。既然数组是矩形的,你为什么不为整个数组分配一块内存呢?@lulle我不知道你怎么能给我提供一个链接或一些有帮助的东西?
free(arr);