next up previous contents
Next: 指標指向指標 Up: 指標(pointers) Previous: 指標算術   Contents

指標陣列

  1. 宣告
    int *ptr[MAX];
    
  2. 範例一:整數指標。
    #include <stdio.h>
     
    const int MAX = 3;
     
    int main ()
    {
       int  var[] = {10, 100, 200};
       int i, *ptr[MAX];
     
       for ( i = 0; i < MAX; i++)
       {
          ptr[i] = &var[i]; /* assign the address of integer. */
       }
       for ( i = 0; i < MAX; i++)
       {
          printf("Value of var[%d] = %d\n", i, *ptr[i] );
       }
       return 0;
    }
    
  3. 範例二:字元指標。
    #include <stdio.h>
     
    const int MAX = 4;
     
    int main ()
    {
       char *names[] = {
                       "Zara Ali",
                       "Hina Ali",
                       "Nuha Ali",
                       "Sara Ali",
       };
       int i = 0;
    
       for ( i = 0; i < MAX; i++)
       {
          printf("Value of names[%d] = %s\n", i, names[i] );
       }
       return 0;
    }
    



2015-01-16