指针与数组 (二)

来源:互联网 发布:c语言告白知乎 编辑:程序博客网 时间:2024/05/17 01:56
 思维导图
 


 
 介绍
 
前接上文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。
复制代码
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中。
复制代码
 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 last25     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型的函数的指针"

0 0