c - C中的半固定,半动态数组

标签 c arrays

有没有办法初始化一半固定、一半动态大小的字符串数组?怎么办?

例子:

char **p[2];

/*Memory allocation here*/

strcpy(p[0][0], "foo");
strcpy(p[0][1], "bar");
strcpy(p[1][0], "baz");  
strcpy(p[1][1], "qux");

最佳答案

是的,有办法。

p[0] = malloc(sizeof(char *) * 2); //Two pointers in p[0]
p[1] = malloc(sizeof(char *) * 2); //Two pointers in p[1]

p[0][0] = malloc(sizeof(char) * 4); //4 bytes in p[0][0]
p[0][1] = malloc(sizeof(char) * 4);
p[1][0] = malloc(sizeof(char) * 4);
p[1][1] = malloc(sizeof(char) * 4);

关于c - C中的半固定,半动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40324749/

相关文章:

PHP:如何将 $_POST 中的多个复选框数组保存在变量中

python Spark 替代非常大的数据爆炸

javascript - 如何使用 angularjs 从对象数组中删除项目?

c - 我对 n*log(N) Big O 的样子很感兴趣

c - 为什么代码不是递归出来的?

c - 为什么 SetFilePointerEx 的第二个参数在编译时给我一个不兼容的类型错误?

c - Micro C OS II 中的时间延迟

C++数组意外错误

c - 柏林噪声方 block 不匹配

javascript - 使数组数字为负