将值分配给使用malloc创建的2D数组 [英] Assigning values to 2D array created using malloc
本文介绍了将值分配给使用malloc创建的2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用malloc创建了2D字符数组,并一直试图将其值分配给'\ 0'.
I created a 2D character array using malloc and have been trying to assign its values to '\0'.
char **predicate_array = malloc(no_of_lines_in_data_map);
for(int i = 0; i < no_of_lines_in_data_map; i++){
predicate_array[i] = malloc(1024 * sizeof(char));
predicate_array[i][0] = '\0';
}
但是,当我打印输出时,我得到:
However, when I print the output I get:
predicate_array[0] = (P;
predicate_array[1] =
predicate_array[2] =
predicate_array[3] =
predicate_array[4] =
predicate_array[5] =
predicate_array[6] =
...
...
有人知道为什么第一个数组未设置为'\ 0'吗?
Would anyone know why the first array is not being set to '\0'?
谢谢.
推荐答案
第一个malloc的参数不适合您的2d数组.
the parameter of the first malloc don't fit your 2d-array.
如下修复
char **predicate_array = (char **)malloc(no_of_lines_in_data_map*sizeof(char *));
这篇关于将值分配给使用malloc创建的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文