So, here's my logic:
所以,这是我的逻辑:
This is some text:
这是一些文字:
char *text;
Then this is array of texts:
然后这是一系列文本:
char **arr;
Then array of these arrays is:
然后这些数组的数组是:
char ***arr2d;
And if I want a function to modify it, it needs to accept it as:
如果我想要一个函数来修改它,它需要接受它:
char ****arr2d;
And within the function use it as:
在函数内使用它:
*arr2d = (e.g. allocate);
So if I want to create 2D array like this and make the first row, first column contain just a letter 'a', then why does this not work?
因此,如果我想创建像这样的2D数组并创建第一行,第一列只包含一个字母'a',那为什么这不起作用?
#define COLUMNS 7
void loadTable(char ****table)
{
*table = (char ***) malloc(sizeof(char**));
if (!*table) {
printf("Allocation error for table rows.");
return;
}
*table[0] = (char**) malloc(COLUMNS * sizeof(char*));
if (!*table[0]) {
printf("Allocation error for table columns.");
return;
}
*table[0][0] = (char*) malloc(2 * sizeof(char));
*table[0][0][0] = (char)(97);
*table[0][0][1] = '\0';
}
int main()
{
char ***table;
loadTable(&table);
return 0;
}
1 个解决方案
#1
2
You would need only 3 ***
to do what you describe, not 4 ****
. Be aware, there are methods to allow you to avoid excessive depth in terms of arrays of arrays of strings. And there are also good reasons to avoid excessively deep orders of arrays, not the least is the need to free(.)
everything you allocate. That means exactly one call to free(.)
for each and every call to [m][c][re]alloc(.)
.
你只需要3 ***来做你所描述的,而不是4 ****。请注意,有一些方法可以避免在字符串数组数组方面过度深度。并且还有充分的理由避免过多的数组命令,最重要的是需要释放(。)你分配的所有内容。这意味着对[m] [c] [re] alloc(。)的每次调用都只有一次调用free(。)。
But to address your question...
但要解决你的问题......
In general, to create new memory for a single array of a previously allocated memory, you could use a function prototyped as:
通常,要为先前分配的内存的单个数组创建新内存,可以使用原型为:
char * ReSizeBuffer(char **str, size_t origSize);
Where if say the previously allocated buffer were created as:
如果说先前分配的缓冲区被创建为:
char *buf = calloc(origSize, 1);
...the usage could look like:
......用法看起来像:
char *tmp = {0};
tmp = ReSizeBuffer(&buf, newSize); //see implementation below
if(!tmp)
{
free(buf);
return NULL;
}
buf = tmp;
///use new buf
...
Then if this works for a single array of char, then the prototype for allocating new memory for a previously allocated array of strings might look like this:
然后,如果这适用于单个char数组,那么为先前分配的字符串数组分配新内存的原型可能如下所示:
char ** ReSizeBuffer(char ***str, size_t numArrays, size_t strLens);
Where if say the previously allocated 2D buffer were created as:
如果说先前分配的2D缓冲区被创建为:
char **buf = Create2DStr(size_t numStrings, size_t maxStrLen); //see implementation below
...the usage could look like:
......用法看起来像:
char **tmp = {0};
tmp = ReSizeBuffer(&buf, numStrings, maxStrLen);
if(!tmp)
{
free(buf);
return NULL;
}
buf = tmp;
///use new buf
...
Implementations:
Implementation of ReSizeBuffer
. This must be expanded if you desire to implement the second prototype:
ReSizeBuffer的实现。如果您希望实现第二个原型,必须扩展它:
char * ReSizeBuffer(char **str, size_t size)
{
char *tmp={0};
if(!(*str)) return NULL;
if(size == 0)
{
free(*str);
return NULL;
}
tmp = (char *)realloc((char *)(*str), size);
if(!tmp)
{
free(*str);
return NULL;
}
*str = tmp;
return *str;
}
Implementation of Create2DStr
might look like this:
Create2DStr的实现可能如下所示:
char ** Create2DStr(size_t numStrings, size_t maxStrLen)
{
int i;
char **a = {0};
a = calloc(numStrings, sizeof(char *));
for(i=0;i<numStrings; i++)
{
a[i] = calloc(maxStrLen + 1, 1);
}
return a;
}
#1
2
You would need only 3 ***
to do what you describe, not 4 ****
. Be aware, there are methods to allow you to avoid excessive depth in terms of arrays of arrays of strings. And there are also good reasons to avoid excessively deep orders of arrays, not the least is the need to free(.)
everything you allocate. That means exactly one call to free(.)
for each and every call to [m][c][re]alloc(.)
.
你只需要3 ***来做你所描述的,而不是4 ****。请注意,有一些方法可以避免在字符串数组数组方面过度深度。并且还有充分的理由避免过多的数组命令,最重要的是需要释放(。)你分配的所有内容。这意味着对[m] [c] [re] alloc(。)的每次调用都只有一次调用free(。)。
But to address your question...
但要解决你的问题......
In general, to create new memory for a single array of a previously allocated memory, you could use a function prototyped as:
通常,要为先前分配的内存的单个数组创建新内存,可以使用原型为:
char * ReSizeBuffer(char **str, size_t origSize);
Where if say the previously allocated buffer were created as:
如果说先前分配的缓冲区被创建为:
char *buf = calloc(origSize, 1);
...the usage could look like:
......用法看起来像:
char *tmp = {0};
tmp = ReSizeBuffer(&buf, newSize); //see implementation below
if(!tmp)
{
free(buf);
return NULL;
}
buf = tmp;
///use new buf
...
Then if this works for a single array of char, then the prototype for allocating new memory for a previously allocated array of strings might look like this:
然后,如果这适用于单个char数组,那么为先前分配的字符串数组分配新内存的原型可能如下所示:
char ** ReSizeBuffer(char ***str, size_t numArrays, size_t strLens);
Where if say the previously allocated 2D buffer were created as:
如果说先前分配的2D缓冲区被创建为:
char **buf = Create2DStr(size_t numStrings, size_t maxStrLen); //see implementation below
...the usage could look like:
......用法看起来像:
char **tmp = {0};
tmp = ReSizeBuffer(&buf, numStrings, maxStrLen);
if(!tmp)
{
free(buf);
return NULL;
}
buf = tmp;
///use new buf
...
Implementations:
Implementation of ReSizeBuffer
. This must be expanded if you desire to implement the second prototype:
ReSizeBuffer的实现。如果您希望实现第二个原型,必须扩展它:
char * ReSizeBuffer(char **str, size_t size)
{
char *tmp={0};
if(!(*str)) return NULL;
if(size == 0)
{
free(*str);
return NULL;
}
tmp = (char *)realloc((char *)(*str), size);
if(!tmp)
{
free(*str);
return NULL;
}
*str = tmp;
return *str;
}
Implementation of Create2DStr
might look like this:
Create2DStr的实现可能如下所示:
char ** Create2DStr(size_t numStrings, size_t maxStrLen)
{
int i;
char **a = {0};
a = calloc(numStrings, sizeof(char *));
for(i=0;i<numStrings; i++)
{
a[i] = calloc(maxStrLen + 1, 1);
}
return a;
}