指针数组的初始化
指针数组的初始化:
指针数组的初始化语法与其他类型对象的初始化语法类似,下面是一个例子:
char *month_name(int n)
{
static char *name[] = {
"Illegal month","January","February","March","April","May","June","july",
"August","September","October","November","December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}
其中name是一个一维数组,数组的元素为字符指针。name数组的初始化通过一个字符串列表实现,列表中的每个字符串赋值给数组相应位置的元素。第i个字符串的所有字符存储在存储器的某个位置,指向它的指针存储在name[i]中。由于上述声明没有指明name的长度,因此编译器会对初值个数进行统计,并将这一准确数字填入数组的长度。