指针与数组 (二)

 思维导图
 


 
 介绍
 
前接上文 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 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型的函数的指针"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值