存储文本行的一种好办法是用一个指针(模拟一个数组)指向一系列的char型指针(每个模拟一个数组)。这种数据结构有时被称为不规则数组
char *a[4] = {"this", "is", "a", "test"};
char **p = a;
#include <stdlib.h>
char **p = malloc(4*sizeof(char *));
if (p != NULL)
{
p[0] = malloc(5);
p[1] = malloc(3);
p[2] = malloc(2);
p[3] = malloc(5);
if (p[0] && p[1] && p[2] && p[3])
{
strcpy(p[0], "this");
strcpy(p[1], "is");
strcpy(p[2], "a");
strcpy(p[3], "test");
}
}
此代码只是伪代码,大概解释,没有规范编写供读者运行。