C語言第十三回合:再談指標,c語言回合指標

來源:互聯網
上載者:User

C語言第十三回合:再談指標,c語言回合指標

C語言第十二回合:再談指標

 

【學習目標】

1.        指標數組

2.        數組指標

3.        函數指標

 

 

( 1 )指標數組

   指標數組:首先它是一個數組,數組的元素都是指標,數組佔多少個位元組由數組本身

決定。它是“儲存指標的數組”的簡稱

   如:

定義數組名為ptr,其中包含10個指向int類型的資料的指標

int *ptr[ 10];  //指標數組,int*修飾數組的內容

 

[程式]

#include <stdio.h> void print_error( int n ); int main( void ){         inti;                 for(i= 0; i< 4; i++ )         {                   print_error(i );         }                 putchar(10 ); //printf("\n" );           return 0; } void print_error( int i ){         //定義包含4個指向int類型的資料指標的數組         char*error[]= {                           "cann`t openfile\n",                                                "read error\n",                                                "write error\n",                                                "media failure\n"                        };                          printf( "%s", error[ i ] );                 return;} 

( 2 )數組指標

   數組指標:首先它是一個指標,它指向一個數組。在 3 2 位系統下永遠是占 4 個位元組,

至於它指向的數組佔多少位元組,不知道。它是“指向數組的指標”的簡稱。

格式:類型 ( *標識符 )[ ] 

// int ( *p )[ 5 ];

 

#include <stdio.h> void print( char ( *ptr )[ 10 ], int n ) //數組指標作為參數{         inti;                 for(i= 0; i< n; i++ )         {                   printf("%s\n", *( ptr+ i ) );         }                 return;}int main( void ){         charname[ 3 ][ 10 ]= {                                  "zhangsan",                                                                            "lisi",                                                                           "wangwu"                                };         print(name, 3 );  //列印           return 0; }

( 3 )函數指標

   函數指標:就是指向函數的指標。

   格式:類型 ( * 標識符 )( );

[程式 ]

#include <stdio.h> //使用指標參數//功能:兩個數交換void swap( int *a, int *b ); int main( void ){  int n= 10;  int m= 20;   //第一指標函數,傳回型別void,f為函數指標  void ( *f )();      f=swap; //f指向swap。注意:類型必須匹配    printf( "交換前:n= %d m=%d\n", n, m );    if( n< m ) //如果n< m,就交換   {        f(&n, &m );   }    printf( "交換後:n= %d m= %d\n", n, m );          return 0;   } void swap( int *a, int *b ){         inttemp;                 temp=*a;         *a=*b;         *b=temp;           return ;   }


運行結果:

交換前:n=10 m= 20

交換後:n=20 m= 10

 


【指尖的微笑】錯誤在所難免,希望得到大家的指正^-^

轉載時保留原文的連結http://codingit.howbbs.com和http://blog.csdn.net/mirrorsbeyourself

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.