有人可以解释一下如何在 c 中为指向字符指针数组的指针正确分配内存吗?例如:
char *(*t)[];
我试着这样做:
*t = malloc( 5 * sizeof(char*));
这给了我一个编译错误:
error: invalid use of array with unspecified bounds
在这方面的任何帮助都会很棒!谢谢
最佳答案
你可以做的是:
char **t = (char**)malloc( <no of elements> * sizeof(char*));
分配指针数组。
for (i = 0 ; i< <no of elements> ; i++)
{
t[i] = (char*)malloc( <length of text> * sizeof(char));
}
为数组的每个元素指向的文本分配内存。
关于c - 如何为指向 char * 数组的指针分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936770/