lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

c:对指针数组、数组指针、char数组、char指针的探究(费头发)

 

 

 

 

一、指针数组

 

 

  1、指针数组:  “指针数组”是“数组”;它是存储指针的数组。

 

 

  2、指针数组的定义:

    2.1、TYPE    *pointer_array[SIZE]

    2.2、" TYPE "是数据类型;" SIZE "是正整数。

    2.3、涵义:pointer_array存储"SIZE"个指针,“SIZE”个指针是"TYPE类型的指针"。

 

 

  3、int *int_pta[10]:int_pta是存储10个指针的数组,这10个指针的是“int类型的指针”。

 

 

  4、代码示例:

 

 1 [root@rocky c]# cat  pointer_array.c 
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 
 6 
 7 
 8 #define SIZEX 5
 9 #define SIZEY 3
10 
11 
12 
13 
14 // array; array of pointers
15 void array_of_pointers()
16 {
17 
18         // num2[3][5]
19         int num2[SIZEY][SIZEX] = {
20                                 {0,1,2,3,4},
21                                 {5, 6,7,8,9},
22                                 {10,11,12,13,14}
23                         };
24 
25         // array of pointers pointer_array[3][5]
26         int *pointer_array[SIZEY][SIZEX] ;
27 
28         // initial pointer_array
29         for(int i=0; i<SIZEY; i++)
30         {
31                 for(int j=0; j< SIZEX; j++)
32                 {
33                         pointer_array[i][j] = &num2[i][j];
34                 }
35         }
36 
37         printf("\n");
38         printf("---- array_of_pointers ----\n");
39         // output pointer_array
40         for(int i=0;i<SIZEY; i++)
41         {
42                 for(int j=0; j< SIZEX; j++)
43                 {
44                         printf("\tpointer_array[%d][%d] = %d,\tnum2[%d][%d] = %d\n", i,j,*pointer_array[i][j],i,j, num2[i][j]);
45                 }
46         }
47         printf("---- array_of_pointers ----\n");
48         printf("\n");
49 }

 

 

 

 

二、数组指针:

 

 

  1、数组指针:  “数组指针”是“指针”;它是指向数组的指针。

 

 

  2、数组指针的定义:

    2.1、TYPE  (*pointer_array)[SIZE]

    2.2、" TYPE "是数据类型;" SIZE1,SIZE2,SIZE3,...,SIZEN "是正整数。

    2.3、" pointer_array "是指向"SIZE"个元素的数组的指针;pointer_array通常指向二维数组array2,这个二维数组通常定义为" array2[][SIZE] "。

    2.4、步进:  即”pointer_array + 1“ 要一次性跨越" SIZE "个数组元素。" pointer_array +  1":通常指向”下一行“元素。

    2.5、指向“0维”数组的指针:

      2.5.1、定义数组: type    array0[0];数组的维数Ds,0维数组的维数:array0_Ds=0;

        2.5.1.1、数组元素:array0[d0];数组名是“array0”,“数组名”是地址常量,它代表固定的内存地址;

        2.5.1.2、d0:  d0=0;

        2.5.1.3、将“0维”数组当成“N维”数组:array0[SIZEN=d0]...[0][0][0];

      2.5.2、定义数组的指针和初始化:  type  (*pt0)[0]=array0;

      2.5.3、指针的说明:

        2.5.3.1、“0维”数组的指针pt0,pt0指向type类型的元素;即“pt0就是指向type类型的普通变量”;

      2.5.4、获取指针的地址:若 pt0 = array0;则地址为:pt0;(地址等价,pt0 == &array0);

      2.5.5、获取指针的数值:若 pt0 =array0;则数值为:*pt0;(数值等价,*pt0 == array0 );        

    2.6、指向”一维“数组的指针:

      2.6.1、定义数组: type   array1[SIZE1];数组的维数Ds,一维数组的维数:array1_Ds=1;

        2.6.1.1、数组元素:array1[d1];数组名是“array1”;“数组名”是地址常量,它代表固定的内存地址;

        2.6.1.2、d1:  0  <=  d1  <  SIZE1;

        2.6.1.3、将“一维”数组当成“N维”数组:array1[SIZEN=d1][SIZE(N-1)=d0]...[0][0][0];

      2.6.2、定义数组指针和初始化:type  (*pt)[0]=array1;

      2.6.3、指针的说明:pt1指向type类型的元素;

      2.6.4、获取地址:

        2.6.4.1、地址偏移的角度[ base+offset ]:若 “pt1 = array1”;则地址为:(pt1+d1);(地址等价,(pt1+d1) == &array1[d1]);

        2.6.4.2、指针变量的角度[ pt = pt+offset ]:若 “pt1 = pt1 + d1”;则地址为: pt1;(地址等价,pt1 == &array1[d1] ); 

      2.6.5、获取数值: 

        2.6.5.1、地址偏移的角度[ base+offset ]:若 “pt1 = array1”;则数值为:*(pt1+d1);(数值等价,  *(pt1 + d1)  ==  array1[d1]  );

        2.6.5.2、指针变量的角度[ pt = pt+offset ]:若 “pt1 =array1;pt1 = pt1 + d1”;则数值为:*pt1;(数值等价,*pt1  == array1[d1] ) ;

    2.7、指向”二维“数组的指针:

      2.7.1、定义数组: type   array2[SIZE2][SIZE1];数组的维数Ds,二维数组的维数:array2_Ds=2;

        2.7.1.1、数组元素:array2[d2][d1];数组名是“array2”;“数组名”是地址常量,它代表固定的内存地址;

          2.7.1.1.1、d1:  0  <=  d1  <  SIZE1;

          2.7.1.1.2、d2:  0  <=  d2  <  SIZE2;

        2.7.1.2、将“二维”数组当成“N维”数组:array2[SIZEN=d2][SIZE(N-1)=d1][SIZE(N-2)=d0]...[0][0][0];

      2.7.2、定义数组指针:type  (*pt2)[SIZE1]=array2;

      2.7.3、指针的说明:

        2.7.3.1、理解数组指针的定义:

          2.7.3.1.1、pt2将“array2”当作“二维”数组:array2[ROW][COLUMN]

            2.7.3.1.1.1、第一部分,ROW=d2  ;

            2.7.3.1.1.2、第二部分,COLUMN=d1  ;

        2.7.3.2、pt2指向:pt2指向array2[ROW],即pt2指向array2每行的首地址;即(地址相等)pt2[d2] == array2[d2];

        2.7.3.3、pt2的步进(地址比较):

          2.7.3.3.1、type  (*pt2)[SIZE1],其中“SIZE1”定义了pt2的步进,即(pt2+1)要跳过元素的数量。

          2.7.3.3.2、pt2步进的解释:  因为array2[COLUMN]有“SIZE1”个元素,故(pt2+1)直接跳到下一行的行首元素地址;

          2.7.3.3.3、若“ pt2 = array2 ”;则“ pt2 = pt2+d2 ”;(地址等价,(pt2+d2)==array2[d2] )。

      2.7.4、获取地址:

        2.7.4.1、地址偏移的角度[ base+offset ]:若 “pt2 = array2”;则地址为:(pt2+d2);(地址等价,(pt2+d2) == array2[d2]); 

        2.7.4.2、指针变量的角度[ pt = pt+offset ]:若 “pt2=array2;pt2 = pt2 + d2”;则地址为: pt2;(地址等价,pt2 == array2[d2] ); 

      2.7.5、获取数值:

        2.7.5.1、地址偏移的角度[ base+offset ]:若 “pt2 = array2”;则数值为:*(pt2+d2);(数值等价,  *(pt2 + d2)  ==  array2[d2][0]  );

        2.7.5.2、指针变量的角度[ pt = pt+offset ]:若 “pt2 =array2;pt2 = pt2 + d2”;则数值为:*pt2;(数值等价,*pt2  == array2[d2][0] ) ;

    2.8、指向”三维“数组的指针:

      2.8.1、定义数组: type   array3[SIZE3][SIZE2][SIZE1];数组的维数Ds,三维数组的维数:array3_Ds=3;

        2.8.1.1、数组元素:array3[d3][d2][d1];数组名是“array3”;“数组名”是地址常量,它代表固定的内存地址;

          2.8.1.1.1、d1:  0  <=  d1  <  SIZE1;

          2.8.1.1.2、d2:  0  <=  d2  <  SIZE2;

          2.8.1.1.3、d3:  0  <=  d3  <  SIZE3;

        2.8.1.2、将“三维”数组当成“N维”数组:array3[SIZEN=d3][SIZE(N-1)=d2][SIZE(N-2)=d1][SIZE(N-3)=d0]...[0][0][0];

      2.8.2、定义数组指针:type   (*pt3)[SIZE2][SIZE1]=array3;

      2.8.3、指针的说明:

        2.8.3.1、理解数组指针的定义:

          2.8.3.1.1、pt3将“array3”当作“二维”数组:array3[ROW][COLUMN]

            2.8.3.1.1.1、第一部分,ROW=d3  ;

            2.8.3.1.1.2、第二部分,COLUMN=d2,d1  ;

        2.8.3.2、pt3指向:pt3指向array3[ROW],即pt3指向array3每行的首地址;即(地址相等)pt3[d3] == array3[d3];

        2.8.3.3、pt3的步进(地址):

          2.8.3.3.1、type  (*pt3)[SIZE2][SIZE1],其中“(SIZE2 * SIZE1)”定义了pt3的步进,即(pt3+1)要跳过元素的数量。

          2.8.3.3.2、pt3步进的解释:  因为array3[COLUMN]有“(SIZE2 * SIZE1)”个元素,故(pt3+1)直接跳到下一行的行首元素地址;

          2.8.3.3.3、若“ pt3 = array3 ”;则“ pt3 = pt3 + array3 ”;则地址为 pt3; (地址等价,pt3 == array3[d3])。

      2.8.4、获取地址:

        2.8.4.1、地址偏移的角度[ base+offset ]:若 “pt3 = array3”;则地址为:(pt3+d3);(地址等价,(pt3+d3) == array3[d3]);

        2.8.4.2、指针变量的角度[ pt = pt+offset ]:若 “pt3=array3;pt3 = pt3 + d3”;则地址为: pt3;(地址等价,pt3 == array3[d3] ); 

      2.8.5、获取数值:

         2.8.5.1、地址偏移的角度[ base+offset ]:若 “pt3 = array3”;则数值为:*(pt3+d3);(数值等价,  *(pt3 + d3)  ==  array3[d3][0][0]  );

         2.8.5.2、指针变量的角度[ pt = pt+offset ]:若 “pt3 =array3;pt3 = pt3 + d3”;则数值为:*pt3;(数值等价,*pt3  == array3[d3][0][0] ) ;

     2.9、指向”n维“数组的指针:

      2.9.1、定义数组: type   arrayN[SIZEN]...[SIZE3][SIZE2][SIZE1];数组的维数Ds,三维数组的维数:arrayN_Ds=N;

        2.9.1.1、数组元素:arrayN[dn]...[d3][d2][d1];数组名是“arrayN”;“数组名”是常量,它代表固定的内存地址;

          2.9.1.1.1、d1:  0  <=  d1  <  SIZE1;

          2.9.1.1.2、d2:  0  <=  d2  <  SIZE2;

          2.9.1.1.3、d3:  0  <=  d3  <  SIZE3;

            .        .

            .        .

            .        .

          2.9.1.1.n、dn:  0  <=  dn  <  SIZEN;

        2.9.1.2、将“N维”数组当成“N维”数组:arrayN[SIZEN=dn]...[SIZE3=d3][SIZE2=d2][SIZE1=d1];

      2.9.2、定义数组指针:type   (*ptN)[SIZE(N-1)]...[SIZE2][SIZE1]=arrayN;

      2.9.3、指针的说明:

        2.9.3.1、理解数组指针的定义:

          2.9.3.1.1、ptN将“arrayN”当作“二维”数组:arrayN[ROW][COLUMN]

            2.9.3.1.1.1、第一部分,ROW=dn  ;

            2.9.3.1.1.2、第二部分,COLUMN=d(n-1), ..., d2,d1  ;

        2.9.3.2、ptN指向:ptN指向arrayN[ROW],即ptN指向arrayN每行的首地址;即(地址相等)ptN[dn] == arrayN[dn](ptN[dn] == arrayN[dn]...[0][0][0]);

        2.9.3.3、ptN的步进(地址):

          2.9.3.3.1、type  (*ptN)[arrayN_Ds-1]...[SIZE2][SIZE1],其中“(SIZE[arrayN_DS-1]*...*SIZE2 * SIZE1)”定义了ptN的步进,即(ptN+1)要跳过元素的数量。

          2.9.3.3.2、ptN步进的解释:  因为arrayN[COLUMN]有“(SIZE[arrayN_DS-1]*...*SIZE2 * SIZE1)”个元素,故(ptN+1)直接跳到下一行的行首元素地址;

          2.9.3.3.3、若“ptN=arrayN;ptN = ptN + dn”;则地址为:ptN (地址等价,ptN == arrayN[dn]))。

      2.9.4、获取地址: 

        2.9.4.1、地址偏移的角度[ base+offset ]:若 “ptN = arrayN”;则地址为:(ptN+dn);(地址等价,(ptN+dn) == arrayN[dn]);

         2.9.4.2、指针变量的角度[ pt = pt+offset ]:若 “ptN=arrayN;ptN = ptN + dn”;则地址为: ptN;(地址等价,ptN == arrayN[dn] ); 

       2.9.5、获取数值:

         2.9.5.1、地址偏移的角度[ base+offset ]:若 “ptN = arrayN”;则数值为:*(ptN+dn);(数值等价,  *(ptN + dn)  ==  arrayN[dn]...[0][0]  );

         2.9.5.2、指针变量的角度[ pt = pt+offset ]:若 “ptN =arrayN;ptN = ptN + dn”;则数值为:*ptN;(数值等价,*ptN  == arrayN[dn]...[0][0] ) ;

 

  

  3、代码示例:

 

 1 [root@rocky c]# cat  pointer_array.c 
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 
 6 
 7 
 8 #define SIZEX 5
 9 #define SIZEY 3
10 
11 
12 
13 
14 // pointer; pointer to array
15 void pointer_of_arrays()
16 {
17 
18         // num2[3][5]
19         int num2[SIZEY][SIZEX] = {
20                                 {0,1,2,3,4},
21                                 {5, 6,7,8,9},
22                                 {10,11,12,13,14}
23                         };
24 
25         // pointer of arrays
26         int (*poa)[SIZEX] ;
27         // initial pointer_array
28         poa=num2;
29 
30         printf("\n");
31         printf("---- pointer_of_arrays ----\n");
32         // output pointer_array
33         for(int i=0;i<SIZEY; i++)
34         {
35                 printf("=1=*(*(poa+%d))= %d,\tnum2[%d][0] = %d\n", i,*(*(poa+i)), i, num2[i][0]);
36                 for(int j=0; j< SIZEX; j++)
37                 {
38                         printf("\t=2=*(*(poa+%d)+%d)= %d,\tnum2[%d][%d] = %d\n", i,j,*(*(poa+i)+j), i,j, num2[i][j]);
39                 }
40         }
41 
42         printf("\n");
43 }

 

 

 

 

三、char数组:

 

 

  1、char数组定义:

    1.1、char string[10]:定义了存储10个“char”的数组;" char string[] "存储一个“字符串”。

    1.2、char  *pts[10]:定义了10个指针,这些指针指向“char数组(字符串)”;"  pts[10] "存储10个“字符串”;

 

 

  2、char数组:它是真正存储“字符串”的位置。

 

 

  3、char数组名:

    3.1、数组名表示数组在内存中的首地址。

    3.2、数组名代表地址常量。数组名不是变量;所以数组名所代表的地址不能被改变,数组名不能被赋值内存地址。

 

 

  4、char数组和字符串(超级重要):

    4.1、用“char”定义字符串数组: 

      4.1.1、char  *string_array[] = {"str0", "str1", "str2"};

      4.1.2、定义指向字符串数组的指针并将该指针初始化:  char   **ptstring = string_array ;

    4.2、获取字符串的值:

      4.2.1、说明(e,element=string):0 =< e < 3

      4.2.2、数组:获取第二个“字符串”:   string_array[e=1]  ;

      4.2.3、指针:获取第二个“字符串”:  *( ptstring+1 );

      4.2.4、指针:获取第二个“字符串”:   ptstring[e=1];

    4.3、重要说明:

      4.3.1、char 定义变量:

        4.3.1.1、字符变量:char   c = 'h' ;

        4.3.1.2、字符数组:char  array_of_chars[5] = {'h', 'e', 'l', 'l', 'o'};

        4.3.1.3、字符串数组:char  *array_of_strings[] = { "string1", "string2", "string3" };    char  **pointer_to_string = array_of_strings ;

      4.3.2、char定义指针:

        4.3.2.1、char定义“0维”指针:char  *pt0 = NULL ;

          4.3.2.1.1、“0维”指针的指向说明:“0维”指针pt0,既可以指向单个字符变量( pt0=&c ),又可以指向“一维”char数组( pt0=array_of_chars );

          4.3.2.1.1、“0维”指针的输出说明:

            4.3.2.1.1.1、pt0输出单个字符: pt0=&c ;  printf( "char=%c\n", *pt0 );

            4.3.2.1.1.1、pt0输出单个字符串: pt0=array_of_chars ;  printf( "char=%s\n", pt0 );

    4.4、代码实例

 1 [root@rocky c]# cat  test_char.c 
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 
 6 // file_name = test_char.c
 7 
 8 
 9 void msg()
10 {
11         char  v1 = 'h';
12         char  *pt1 = &v1;
13 
14         char  va[6] = {'h', 'e', 'l', 'l', 'o'} ;
15         char  *pa = va;         // va == &va[0];
16 
17         char  *vs[4] = {"string0", "string1", "string2" };
18         char  **ps = vs;
19 
20         printf("\nv1_address=%p,\tv1_value=%c\n", pt1, *pt1);
21         printf("\nva_pa_address=%p, \tva_pa_value=%s\t\n", pa, pa);
22         printf("\nvs_ps_address=%p, \tvs_ps_value=%s\t\n", ps[2], ps[2]);
23 }
24 
25 
26 // testing
27 int main(int argc, char *argv[], char *envp[])
28 {
29 
30         msg();
31 
32         return 0;
33 
34 }
35 [root@rocky c]# 
36 [root@rocky c]# 
37 [root@rocky c]# ./test_char 
38 
39 v1_address=0x7ffdf2c819f7,      v1_value=h
40 
41 va_pa_address=0x7ffdf2c819f1,   va_pa_value=hello
42 
43 vs_ps_address=0x402020,         vs_ps_value=string2
44 [root@rocky c]# 
45 [root@rocky c]# 

 

 

 

  5、代码示例:

 

 1 [root@rocky c]# cat  pointer_array.c 
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 
 6 
 7 
 8 #define SIZEX 5
 9 #define SIZEY 3
10 
11 
12 
13 
14 // array, array with chars
15 // fptac = function pointer to array of chars
16 void fptac()
17 {
18 
19         // array of chars
20         char ca[SIZEX] = {'h','e','l','l','o' };
21 
22         // pointer to array of chars
23         char *pca = ca;
24 
25         printf("\n");
26         printf("---- function pointer to array of chars ----\n");
27         for(int i=0; i<SIZEX; i++ )
28         {
29                 printf("\tpca+%i=%c,\tca[%d]=%c\n", i, *(pca+i), i, ca[i]);
30         }
31         printf("\n");
32 
33 }

 

 

 

 

四、char 指针

 

 

  1、char指针的定义

    1.1、char  *pt:  pt指向"char数组"的指针;即 pt指向“字符串”。

    1.2、char  *str[10](指针数组):  str指向10个“字符串”的数组。

    1.3、char   **pts:  pts指向存储“字符串”的数组;pts可以操作“字符串”。

 

 

  2、char指针:指针仅仅是存储地址的变量。

 

 

 

  3、代码示例

 

 1 [root@rocky c]# cat  pointer_array.c 
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 
 6 
 7 
 8 #define SIZEX 5
 9 #define SIZEY 3
10 
11 
12 
13 
14 // array, array with strings
15 // fptas = function point to array of strings
16 void fptas()
17 {
18   int size = 5;
19   char *sa[] = { "hello0", "hello1", "hello2", "hello3", "hello4" };
20   char **psa = sa;
21 
22   printf("\n");
23   printf("***** fptas()  *****\n");
24   for(int i=0; i < size; i++)
25   {
26     printf("\t*(psa+%d)=%s,\tpsa[%d]=%s,\tsa[%d]=%s\n", i, *(psa+i), i, psa[i], i, sa[i]);
27   }
28   printf("***** fptas()  *****\n");
29   printf("\n");
30 }

 

 

 

 

五、完整代码

 

  1 [root@rocky c]# cat  pointer_array.c 
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 
  6 
  7 
  8 #define SIZEX 5
  9 #define SIZEY 3
 10 
 11 
 12 
 13 
 14 // array; array of pointers
 15 void array_of_pointers()
 16 {
 17 
 18         // num2[3][5]
 19         int num2[SIZEY][SIZEX] = {
 20                                 {0,1,2,3,4},
 21                                 {5, 6,7,8,9},
 22                                 {10,11,12,13,14}
 23                         };
 24 
 25         // array of pointers pointer_array[3][5]
 26         int *pointer_array[SIZEY][SIZEX] ;
 27 
 28         // initial pointer_array
 29         for(int i=0; i<SIZEY; i++)
 30         {
 31                 for(int j=0; j< SIZEX; j++)
 32                 {
 33                         pointer_array[i][j] = &num2[i][j];
 34                 }
 35         }
 36 
 37         printf("\n");
 38         printf("---- array_of_pointers ----\n");
 39         // output pointer_array
 40         for(int i=0;i<SIZEY; i++)
 41         {
 42                 for(int j=0; j< SIZEX; j++)
 43                 {
 44                         printf("\tpointer_array[%d][%d] = %d,\tnum2[%d][%d] = %d\n", i,j,*pointer_array[i][j],i,j, num2[i][j]);
 45                 }
 46         }
 47         printf("---- array_of_pointers ----\n");
 48         printf("\n");
 49 }
 50 
 51 
 52 
 53 
 54 // pointer; pointer to array
 55 void pointer_of_arrays()
 56 {
 57 
 58         // num2[3][5]
 59         int num2[SIZEY][SIZEX] = {
 60                                 {0,1,2,3,4},
 61                                 {5, 6,7,8,9},
 62                                 {10,11,12,13,14}
 63                         };
 64 
 65         // pointer of arrays
 66         int (*poa)[SIZEX] ;
 67         // initial pointer_array
 68         poa=num2;
 69 
 70         printf("\n");
 71         printf("---- pointer_of_arrays ----\n");
 72         // output pointer_array
 73         for(int i=0;i<SIZEY; i++)
 74         {
 75                 printf("=1=*(*(poa+%d))= %d,\tnum2[%d][0] = %d\n", i,*(*(poa+i)), i, num2[i][0]);
 76                 for(int j=0; j< SIZEX; j++)
 77                 {
 78                         printf("\t=2=*(*(poa+%d)+%d)= %d,\tnum2[%d][%d] = %d\n", i,j,*(*(poa+i)+j), i,j, num2[i][j]);
 79                 }
 80         }
 81 
 82         printf("\n");
 83 }
 84 
 85 
 86 
 87 
 88 // array, array with chars
 89 // fptac = function pointer to array of chars
 90 void fptac()
 91 {
 92 
 93         // array of chars
 94         char ca[SIZEX] = {'h','e','l','l','o' };
 95 
 96         // pointer to array of chars
 97         char *pca = ca;
 98 
 99         printf("\n");
100         printf("---- function pointer to array of chars ----\n");
101         for(int i=0; i<SIZEX; i++ )
102         {
103                 printf("\tpca+%i=%c,\tca[%d]=%c\n", i, *(pca+i), i, ca[i]);
104         }
105         printf("\n");
106 
107 }
108 
109 
110 
111 
112 // array, array with strings
113 // fptas = function point to array of strings
114 void fptas()
115 {
116   int size = 5;
117   char *sa[] = { "hello0", "hello1", "hello2", "hello3", "hello4" };
118   char **psa = sa;
119 
120   printf("\n");
121   printf("***** fptas()  *****\n");
122   for(int i=0; i < size; i++)
123   {
124     printf("\t*(psa+%d)=%s,\tpsa[%d]=%s,\tsa[%d]=%s\n", i, *(psa+i), i, psa[i], i, sa[i]);
125   }
126   printf("***** fptas()  *****\n");
127   printf("\n");
128 }
129 
130 
131 
132 
133 // testing part
134 int main(int argc, char *argv[], char *envp[])
135 {
136 
137         array_of_pointers();
138 
139         pointer_of_arrays();
140 
141         fptac();
142 
143         fptas();
144 
145 
146         return 0;
147 }
148 [root@rocky c]# 
149 [root@rocky c]# 

 

 

 

 

六、运行实例

 

  1 [root@rocky c]# cat  pointer_array.c 
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 
  6 
  7 
  8 #define SIZEX 5
  9 #define SIZEY 3
 10 
 11 
 12 
 13 
 14 // array; array of pointers
 15 void array_of_pointers()
 16 {
 17 
 18         // num2[3][5]
 19         int num2[SIZEY][SIZEX] = {
 20                                 {0,1,2,3,4},
 21                                 {5, 6,7,8,9},
 22                                 {10,11,12,13,14}
 23                         };
 24 
 25         // array of pointers pointer_array[3][5]
 26         int *pointer_array[SIZEY][SIZEX] ;
 27 
 28         // initial pointer_array
 29         for(int i=0; i<SIZEY; i++)
 30         {
 31                 for(int j=0; j< SIZEX; j++)
 32                 {
 33                         pointer_array[i][j] = &num2[i][j];
 34                 }
 35         }
 36 
 37         printf("\n");
 38         printf("---- array_of_pointers ----\n");
 39         // output pointer_array
 40         for(int i=0;i<SIZEY; i++)
 41         {
 42                 for(int j=0; j< SIZEX; j++)
 43                 {
 44                         printf("\tpointer_array[%d][%d] = %d,\tnum2[%d][%d] = %d\n", i,j,*pointer_array[i][j],i,j, num2[i][j]);
 45                 }
 46         }
 47         printf("---- array_of_pointers ----\n");
 48         printf("\n");
 49 }
 50 
 51 
 52 
 53 
 54 // pointer; pointer to array
 55 void pointer_of_arrays()
 56 {
 57 
 58         // num2[3][5]
 59         int num2[SIZEY][SIZEX] = {
 60                                 {0,1,2,3,4},
 61                                 {5, 6,7,8,9},
 62                                 {10,11,12,13,14}
 63                         };
 64 
 65         // pointer of arrays
 66         int (*poa)[SIZEX] ;
 67         // initial pointer_array
 68         poa=num2;
 69 
 70         printf("\n");
 71         printf("---- pointer_of_arrays ----\n");
 72         // output pointer_array
 73         for(int i=0;i<SIZEY; i++)
 74         {
 75                 printf("=1=*(*(poa+%d))= %d,\tnum2[%d][0] = %d\n", i,*(*(poa+i)), i, num2[i][0]);
 76                 for(int j=0; j< SIZEX; j++)
 77                 {
 78                         printf("\t=2=*(*(poa+%d)+%d)= %d,\tnum2[%d][%d] = %d\n", i,j,*(*(poa+i)+j), i,j, num2[i][j]);
 79                 }
 80         }
 81 
 82         printf("\n");
 83 }
 84 
 85 
 86 
 87 
 88 // array, array with chars
 89 // fptac = function pointer to array of chars
 90 void fptac()
 91 {
 92 
 93         // array of chars
 94         char ca[SIZEX] = {'h','e','l','l','o' };
 95 
 96         // pointer to array of chars
 97         char *pca = ca;
 98 
 99         printf("\n");
100         printf("---- function pointer to array of chars ----\n");
101         for(int i=0; i<SIZEX; i++ )
102         {
103                 printf("\tpca+%i=%c,\tca[%d]=%c\n", i, *(pca+i), i, ca[i]);
104         }
105         printf("\n");
106 
107 }
108 
109 
110 
111 
112 // array, array with strings
113 // fptas = function point to array of strings
114 void fptas()
115 {
116   int size = 5;
117   char *sa[] = { "hello0", "hello1", "hello2", "hello3", "hello4" };
118   char **psa = sa;
119 
120   printf("\n");
121   printf("***** fptas()  *****\n");
122   for(int i=0; i < size; i++)
123   {
124     printf("\t*(psa+%d)=%s,\tpsa[%d]=%s,\tsa[%d]=%s\n", i, *(psa+i), i, psa[i], i, sa[i]);
125   }
126   printf("***** fptas()  *****\n");
127   printf("\n");
128 }
129 
130 
131 
132 
133 // testing part
134 int main(int argc, char *argv[], char *envp[])
135 {
136 
137         array_of_pointers();
138 
139         pointer_of_arrays();
140 
141         fptac();
142 
143         fptas();
144 
145 
146         return 0;
147 }
148 [root@rocky c]# 
149 [root@rocky c]# 
150 [root@rocky c]# ./pointer_array 
151 
152 ---- array_of_pointers ----
153         pointer_array[0][0] = 0,        num2[0][0] = 0
154         pointer_array[0][1] = 1,        num2[0][1] = 1
155         pointer_array[0][2] = 2,        num2[0][2] = 2
156         pointer_array[0][3] = 3,        num2[0][3] = 3
157         pointer_array[0][4] = 4,        num2[0][4] = 4
158         pointer_array[1][0] = 5,        num2[1][0] = 5
159         pointer_array[1][1] = 6,        num2[1][1] = 6
160         pointer_array[1][2] = 7,        num2[1][2] = 7
161         pointer_array[1][3] = 8,        num2[1][3] = 8
162         pointer_array[1][4] = 9,        num2[1][4] = 9
163         pointer_array[2][0] = 10,       num2[2][0] = 10
164         pointer_array[2][1] = 11,       num2[2][1] = 11
165         pointer_array[2][2] = 12,       num2[2][2] = 12
166         pointer_array[2][3] = 13,       num2[2][3] = 13
167         pointer_array[2][4] = 14,       num2[2][4] = 14
168 ---- array_of_pointers ----
169 
170 
171 ---- pointer_of_arrays ----
172 =1=*(*(poa+0))= 0,      num2[0][0] = 0
173         =2=*(*(poa+0)+0)= 0,    num2[0][0] = 0
174         =2=*(*(poa+0)+1)= 1,    num2[0][1] = 1
175         =2=*(*(poa+0)+2)= 2,    num2[0][2] = 2
176         =2=*(*(poa+0)+3)= 3,    num2[0][3] = 3
177         =2=*(*(poa+0)+4)= 4,    num2[0][4] = 4
178 =1=*(*(poa+1))= 5,      num2[1][0] = 5
179         =2=*(*(poa+1)+0)= 5,    num2[1][0] = 5
180         =2=*(*(poa+1)+1)= 6,    num2[1][1] = 6
181         =2=*(*(poa+1)+2)= 7,    num2[1][2] = 7
182         =2=*(*(poa+1)+3)= 8,    num2[1][3] = 8
183         =2=*(*(poa+1)+4)= 9,    num2[1][4] = 9
184 =1=*(*(poa+2))= 10,     num2[2][0] = 10
185         =2=*(*(poa+2)+0)= 10,   num2[2][0] = 10
186         =2=*(*(poa+2)+1)= 11,   num2[2][1] = 11
187         =2=*(*(poa+2)+2)= 12,   num2[2][2] = 12
188         =2=*(*(poa+2)+3)= 13,   num2[2][3] = 13
189         =2=*(*(poa+2)+4)= 14,   num2[2][4] = 14
190 
191 
192 ---- function pointer to array of chars ----
193         pca+0=h,        ca[0]=h
194         pca+1=e,        ca[1]=e
195         pca+2=l,        ca[2]=l
196         pca+3=l,        ca[3]=l
197         pca+4=o,        ca[4]=o
198 
199 
200 ***** fptas()  *****
201         *(psa+0)=hello0,        psa[0]=hello0,  sa[0]=hello0
202         *(psa+1)=hello1,        psa[1]=hello1,  sa[1]=hello1
203         *(psa+2)=hello2,        psa[2]=hello2,  sa[2]=hello2
204         *(psa+3)=hello3,        psa[3]=hello3,  sa[3]=hello3
205         *(psa+4)=hello4,        psa[4]=hello4,  sa[4]=hello4
206 ***** fptas()  *****
207 
208 [root@rocky c]# 
209 [root@rocky c]# 

 

 

posted on 2022-12-28 05:31  lnlidawei  阅读(96)  评论(0编辑  收藏  举报