C语言 二维字符数组的动态内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2614249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamic memory for 2D char array
提问by Ankur
I have declared an array char **arr; How to initialize the memory for the 2D char array.
我已经声明了一个数组 char **arr; 如何初始化二维字符数组的内存。
回答by
One way is to do the following:
一种方法是执行以下操作:
char **arr = (char**) calloc(num_elements, sizeof(char*));
for ( i = 0; i < num_elements; i++ )
{
arr[i] = (char*) calloc(num_elements_sub, sizeof(char));
}
It's fairly clear what's happening here - firstly, you are initialising an array of pointers, then for each pointer in this array you are allocating an array of characters.
很清楚这里发生了什么 - 首先,您正在初始化一个指针数组,然后为该数组中的每个指针分配一个字符数组。
You could wrap this up in a function. You'll need to free() them too, after usage, like this:
您可以将其包装在一个函数中。使用后,您也需要 free() 它们,如下所示:
for ( i = 0; i < num_elements; i++ )
{
free(arr[i]);
}
free(arr);
I think this the easiest way to do things and matches what you need.
我认为这是做事最简单的方法,并且符合您的需要。
回答by Alex Jasmin
There are two options for allocating an array of type char **
分配类型数组有两种选择 char **
I've transcribed these 2 code samples from the comp.lang.c FAQ(which also contains a nice illustration of these two array types)
我已经从comp.lang.c 常见问题解答中转录了这两个代码示例(其中还包含这两种数组类型的一个很好的说明)
Option 1- Do one allocation per rowplus one for the row pointers.
选项 1-每行进行一次分配,并为行指针进行一次分配。
char **array1 = malloc(nrows * sizeof(char *)); // Allocate row pointers
for(i = 0; i < nrows; i++)
array1[i] = malloc(ncolumns * sizeof(char)); // Allocate each row separately
Option 2- Allocate all the elements togetherand allocate the row pointers:
选项 2-一起分配所有元素并分配行指针:
char **array2 = malloc(nrows * sizeof(char *)); // Allocate the row pointers
array2[0] = malloc(nrows * ncolumns * sizeof(char)); // Allocate all the elements
for(i = 1; i < nrows; i++)
array2[i] = array2[0] + i * ncolumns;
You can also allocate only one memory blockand use some arithmetic to get at element [i,j]. But then you'd use a char*not a char**and the code gets complicated. e.g. arr[3*ncolumns + 2]instead of arr[3][2]
您也可以只分配一个内存块并使用一些算法来获取 element [i,j]。但是你会使用char*not achar**并且代码变得复杂。例如arr[3*ncolumns + 2]代替arr[3][2]
回答by ndim
You might be better off with a one dimensional array:
使用一维数组可能会更好:
char *arr = calloc(WIDTH*HEIGHT, sizeof(arr[0]));
for (int y=0; y<HEIGHT; y++)
for (int x=0; x<WIDTH; x++)
arr[WIDTH*y+x] = 2*arr[WIDTH*y+x];
free(arr);
回答by adrian
Use the following trick:
使用以下技巧:
typedef char char2D[1][1];
char2D *ptr;
ptr = malloc(rows * columns, sizeof(char));
for(i = 0; i < rows; i++)
for(j = 0; j < columns; j++)
(*ptr)[i][j] = char_value;
回答by Kavita Jain
char **array;
int row,column;
char temp='A';
printf("enter the row");
scanf("%d",&row);
printf("enter the column");
scanf("%d",&column);
array=(char **)malloc(row*sizeof(char *));
for (int i=0;i<row;i++)
{
array[i]=(char*)malloc(column*sizeof(char));
}
回答by Rahul
By 2D char array, if you mean a matrix of strings then it may be done in the following way.
对于二维字符数组,如果您指的是字符串矩阵,则可以通过以下方式完成。
int nChars = 25; // assuming a max length of 25 chars per string
int nRows = 4;
int nCols = 6;
char *** arr = malloc(nRows * sizeof(char **));
int i;
int j;
for(i = 0; i < nCols; ++i)
{
arr[i] = malloc(nCols * sizeof(char *));
}
for(i = 0; i < nRows; ++i)
{
for(j = 0; j < nCols; ++j)
{
arr[i][j] = malloc(nChars * sizeof(char));
sprintf(arr[i][j], "Row %d Col %d", i, j);
}
}
To print the 2D char array(matrix of strings(char arrays))
打印二维字符数组(字符串矩阵(字符数组))
for(i = 0; i < nRows; ++i)
{
for(j = 0; j < nCols; ++j)
{
printf("%s ", arr[i][j]);
}
printf("\n");
}
Result is
结果是
Row 0 Col 0 Row 0 Col 1 Row 0 Col 2 Row 0 Col 3 Row 0 Col 4 Row 0 Col 5
Row 1 Col 0 Row 1 Col 1 Row 1 Col 2 Row 1 Col 3 Row 1 Col 4 Row 1 Col 5
Row 2 Col 0 Row 2 Col 1 Row 2 Col 2 Row 2 Col 3 Row 2 Col 4 Row 2 Col 5
Row 3 Col 0 Row 3 Col 1 Row 3 Col 2 Row 3 Col 3 Row 3 Col 4 Row 3 Col 5

