如何为指向 char * 数组的指针分配内存?

ice*_*Tea 1 c memory allocation multidimensional-array

有人可以解释一下如何为指向c中字符指针数组的指针正确分配内存吗?例如:

char *(*t)[];
Run Code Online (Sandbox Code Playgroud)

我尝试这样做:

*t = malloc( 5 * sizeof(char*));
Run Code Online (Sandbox Code Playgroud)

这给了我一个编译错误:

error: invalid use of array with unspecified bounds
Run Code Online (Sandbox Code Playgroud)

对此的任何帮助都会很棒!谢谢

Jan*_*n S 5

你可以做的是:

char **t = (char**)malloc( <no of elements> * sizeof(char*));
Run Code Online (Sandbox Code Playgroud)

这分配了指针数组。

for (i = 0 ; i< <no of elements> ; i++)
{
    t[i] = (char*)malloc( <length of text> * sizeof(char));
}
Run Code Online (Sandbox Code Playgroud)

这为数组的每个元素指向的文本分配内存。