学习《C和指针》笔记

在看C和指针这本书的时候,第六章看到关于在列表中的字符串中查找一个特定的字符的两个版本,对版本二提到的会破坏指针数组的原因进行了分析,先上程序:

/* 版本一 */
#include <stdio.h>

#define TRUE    1
#define FALSE   0

int find_char( char **strings, char value )
{
    char * string;
    while ( ( string = *strings++ ) != NULL )
    {
        while ( *string != '\0' )
        {
            if ( *string++ == value )
                return TRUE;
        }
    }

    return FALSE;
}   

/* 版本二 */
#include <stdio.h>
#include <assert.h>

#define TRUE    1
#define FALSE   0

int find_char( char **strings, int value )
{
    assert( strings != NULL );
    while ( *strings != NULL )
    {
        while ( **strings != '\0' )
        {
            if ( *(*strings)++ == value )
                return TRUE;
        }
        strings++
    }
    return FALSE;
}

char ** 是指向指针的指针,这这里看做是指针数组,数组中的元素是指向char的指针,而且都是指向各个字符串的首地址。
书中说版本二的程序会破坏这个指针数组,是由于*(*strings)++这个表达式中,(*strings)指向的是字符串的首地址,执行*(*strings)++的时候,++的优先级高于*,因此(*strings) 会先将它的值++,会使得(*strings)指向字符串的第二个元素,所以破坏了指针数组。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值