C 雜談之 指標與數組 (二)C 雜談之 指標與數組 (一)

來源:互聯網
上載者:User
 

  思維導圖 

  介紹 前接上文C 雜談之 指標與數組 (一),接續往下談指標和數組。  指標與數組  ——承接上文進行擴充

你知道X = Y,在編譯運行過程中,是什麼樣嗎?

 

 字元指標與函數 1> 字串是一個以'\0'結尾的字元數組。看一個例子:printf接受的是一個指向字元數組第一個字元的指標。

這個例子與下面兩個代碼是一個道理.

2> 幾個常用字元函數的編寫。

 1>>> strcat(s,t)函數,把t指向的字元複製到s指向的字元後面?——注意'\0'

#include <stdio.h>
#include <assert.h>

/* strcat(ps, t): Copy the charactor pointed
* by t append to the character pointed by s
*/
void *strcat(char *ps, char *t){
char *addr = ps;
assert((ps != NULL) && (t != NULL));
while(*ps){ /* The s point to the last character */
ps++;
}

while(*ps++ = *t++){ /* Copy t append to the s*/
}

return addr;
}

int main(){
char s[5] = "AB";
char *t = "E";
printf("%s\n", strcat(s, t));
return 0;
}

注意:在strcat裡的兩個指標ps和t,方法結束後,這兩個函數都指向'\0'後面,因為ps++操作是"先取結果,後自增"
記憶體變化:

金玉嵐

   2>>> 函數strend(s,t):字串t出現在字串s的尾部,返回1,否則返回0。strend

int strlen(char *p){
int iLen = 0;
while(*p){
iLen++;
p++;
}

return iLen;
}

/* strend:find t in s */
int strend(char *s, char *t){
int sLen = strlen(s);
int tLen = strlen(t);

if(tLen > sLen) return 0;

while(*s)
s++;

while(*t)
t++;

for(;tLen--;){
if(*--t != *--s) return 0;
}

return 1;
}

int main(){
char *s = "Hell Wold , Chuanshanjia";
char *t = "Chuanshanjia";

printf("%d\n", strend(s, t));
return 0;
}
  3>>> strncpy(s,t,n) 將t中前n個字元複製到s中。strncpy(s,t,n)

 1 #include <stdio.h>
2
3 int strlen(char *p){
4 int i = 0;
5 while(*p){
6 p++;
7 i++;
8 }
9
10 return i;
11 }
12
13 /* t中最多前n個字元複製到s中 */
14 char *strncpy(char *s, char *t, int n){
15 char *addr = s;
16 if(!strlen(s) || !n) return 0;
17
18 int tLen = strlen(t);
19 if(!tLen) return 0;
20
21 if(tLen < n)
22 n = tLen;
23
24 // Move the pointer s to the last
25 while(*++s) ;
26
27 while(n--){
28 *s++ = *t++;
29 }
30
31 return addr;
32
33 }
34
35
36
37 int main(){
38 char s[20] = "Hell World ";
39 char *t = "Chuanshanjia";
40
41 printf("%s\n", strncpy(s, t, 2222));
42 return 0;
43 }
  命令列參數 1.第一個參數(常用argc表示)(:運行時命令列參數數目;第二個參數(常用argv表示)是一個指向字串數組的指標。2.C語言規定,argv[0],表示啟動程式的名稱。3.執行個體

運行結果:

 記憶體配置:

 

解釋:

>> argv是一個指向指標數組的指標。

所以接受參數也可以寫成:

結合,我們可以這樣理解:從裡向外看

 

 複雜聲明

 1.閱讀——從右向左規則。

>> 規則符號:

-----------------------------------------------------------

* 讀作"指向...的指標"

[] 讀作"...的數組"

() 讀作"返回...的函數"

-----------------------------------------------------------

下面兩個樣本:

int *f() ; // f: 返回指向int型的指標

>>步驟:

1)找標識符f:讀作"f是..."

2)向右看,發現"()"讀作"f是返回...的函數"

3)向右看沒有什麼,向左看,發現*,讀作"f是返回指向...的指標的函數"

4)繼續向左看,發現int,讀作"f是返回指向int型的指標的函數"

 

int (*pf)(); // pf是一個指標——指向傳回值為int型的函數

1)標識符pf,讀作“pf是...”

2)向右看,發現),向左看,發現*,讀作 "pf是指向...的指標"

3)向右看,發現"()",讀作“pf是指向返回...的函數的指標"

4)向右看,沒有,向左看發現int,讀作"pf是指向返回int型的函數的指標"

  總結 我最近著重看C,是因為我想深入瞭解一下PHP核心語言和伺服器模組開發。現在的C語言,能夠讓我更深入的瞭解電腦內部。

 

相關文章

聯繫我們

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