C语言的指针数组


声明: char *lineptr[MAXLINES]

它表示lineptr是一个具有MAXLINES个元素的一維数组,其中数组的每一个元素是一个指向字符类型对象的指针,也就是说,lineptr[i]是一个字符指针,而*lineptr[i]是该指针指向的第i个文本行的首字符.

例:指针数组的初始化(摘自C程序设计)

编写一个函数month_name(n),它返回一个指向第n个月名字的字符串的指针(这也是内部static类型数组的一种理想应用)

程序如下:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
 
char *month_name(int n) 

        static char *name[13] = { 
                "Illegal month", 
                "January", "February", "March", 
                "April", "May", "June", 
                "July", "August", "September", 
                "October", "November", "December" 
        };   
        return (n < 1 || n > 12) ? name[0] : name[n]; 

 
int main() 

        int mon; 
        char *monthname; 
        printf("please input the month name :\n"); 
        scanf("%d", &mon); 
        monthname = month_name(mon); 
        printf("%s\n", monthname); 
        return 0; 

其中name的声明是一个一維数组,数组的元素为字符指针,第i个字符串的所有字符存储在存储器中的某个位置,指向它的指针存储在name[i]中,由于上述声明中没有指明数组name的长度,因此,编译器编译时将对初值个数进行统计,并将这一准确数字填入数组长度.

相关内容