有没有办法初始化一半固定、一半动态大小的字符串数组?怎么办?
例子:
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/