指针数组的初始化和遍历,并且通过for循环方式、函数传参方式进行指针数组的遍历

 1 /*************************************************************************
 2     > File Name: message.c
 3     > Author: Mr.Yang
 4     > Purpose:初始化并使用char指针数组,并且通过for循环方式、函数传参方式进行指针数组的遍历 
 5     > Created Time: 2017年06月04日 星期日 10时15分16秒
 6  ************************************************************************/
 7 
 8 #include <stdio.h>
 9 #include <stdlib.h>
10 
11 /*函数原型*/
12 void print_strings(char *p[],int n);
13 
14 int main(void)
15 {
16         /*声明一个指针数组*/
17         char *message[8] =
18         {
19                 "Four","score","and","seven","years","ago","our","forefathers"
20         };
21 
22         /*用for循环的方式遍历*/
23         int count = 0;
24         for(;count < 8;count++)
25         {
26                 printf("for循环方式遍历:%s\n",message[count]);
27         }
28 
29 
30         /*用函数传参的方式遍历,参数是指针数组名称和元素个数*/
31         print_strings(message,8);
32 
33         return 0;
34 }
35 
36 void print_strings(char *p[],int n)
37 {
38         int i = 0;
39         for(;i < n;i++)
40         {
41                 printf("函数传参方式遍历:%s\n",p[i]);
42         }
43 }

 

posted @ 2017-06-04 10:26  杨来  阅读(1459)  评论(0编辑  收藏  举报