C語言——指標型函數(返回指標值得函數)

來源:互聯網
上載者:User
1 函數類型 *函數名(形式參數表) //指標型函數就是傳回值為指標地址的函數
2 { 函數體 }

函數執行後可以帶回一個值給主調函數 ,這個值可以是標準類型的各種資料 , 也可以是不同類型的指標資料 ,然後使用這些指標間接的訪問相關的資料

如函數strchr()函數,它的功能是在一個字串中尋找一個字元 , 如果找到了則返回該字元的地址 ,否則返回null 指標

 1 #include <stdio.h>
2
3 char *strchr(char *str , char ch)
4 {
5 while(*str != ch && *str != '\0')
6 {
7 str++ ;
8 }
9
10 return str ;
11 }
12
13 int main()
14 {
15 char str[] = "Liang yan" ;
16 char *p , ch ;
17
18 printf("Please input ch :");
19 scanf("%c" , &ch);
20
21 p = strchr(str , ch);
22
23 if(*p)
24 {
25 printf("以8進位格式輸出字串str首地址:%o \n" , str);
26 printf("字元: %c \t 字元 %c在字串str中的地址 :%o \n" , ch , ch , p);
27 printf("該字元在字串中的位置:%d\n" , p - str);
28 }
29 else
30 printf("Not found !\n");
31
32 return 0 ;
33 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.