二重指针以及多重指针的分析方法

u-boot中有这么一段代码。

/*这里定义了一个新的数据类型init_fnc_t

 *这个数据类型是参数为空,返回值为int的函数。
 */

typedef int (init_fnc_t) (void);
/*init_sequence是一个指针数组,指向的是init_fnc_t类型的函数*/
init_fnc_t *init_sequence[] = {
    cpu_init, /* basic cpu dependent setup */
    board_init, /* basic board dependent setup */
    interrupt_init, /* set up exceptions */
    env_init, /* initialize environment */
    init_baudrate, /* initialze baudrate settings */
    serial_init, /* serial communications setup */
    console_init_f, /* stage 1 init of console */
    display_banner, /* say that we are here */
    dram_init, /* configure available RAM banks */
    display_dram_config,
#if defined(CONFIG_VCMA9) || defined (CONFIG_CMC_PU2)
    checkboard,
#endif
    NULL,
};

/*init_fnc_ptr为指向函数指针的指针*/
init_fnc_t **init_fnc_ptr;
/*init_fnc_ptr初始化指向init_sequence指针数组,下面的循环遇到NULL结束*/
for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
    if ((*init_fnc_ptr)() != 0) {/*(*init_fnc_ptr)()为C中调用指针指向的函数*/
        hang ();
    }
}


自己写了2个test程序
一个typedef int (test_fnc_t) (void);
一个typedef int (*test_fnc_t) (void);

#include<stdio.h>

int test0 (void);
int test1 (void);

typedef int (*test_fnc_t) (void);

test_fnc_t test_sequence[] = {
    test0,        
    test1,        
    NULL,
};


//int _tmain(int argc, _TCHAR* argv[])

int main()
{
    test_fnc_t *test_fnc_ptr;

    for (test_fnc_ptr = test_sequence; *test_fnc_ptr; ++test_fnc_ptr) {
        if ((*test_fnc_ptr)() != 0) {
            printf("error here!");
        }
    }

    return 0;
}

int test0 (void)
{
    printf("test0/n");
    return 0;
}

int test1 (void)
{
    printf("test1/n");
    return 0;
}

#include<stdio.h>

int test0 (void);
int test1 (void);


typedef int (test_fnc_t) (void);

test_fnc_t *test_sequence[] = {
    test0,        
    test1,        
    NULL,
};


//int _tmain(int argc, _TCHAR* argv[])

int main()
{
    test_fnc_t **test_fnc_ptr;

    for (test_fnc_ptr = test_sequence; *test_fnc_ptr; ++test_fnc_ptr) {
        if ((*test_fnc_ptr)() != 0) {
            printf("error here!");
        }
    }

    return 0;
}

int test0 (void)
{
    printf("test0/n");
    return 0;
}

int test1 (void)
{
    printf("test1/n");
    return 0;

}



typedef int (init_fnc_t) (void);

上面使用到了二重指针init_fnc_t **init_fnc_ptr; 怎么理解呢?可以这么理解,

首先定性为这是一个指针,也就是占四个字节内存单元,里面存的数字是一个指针(地

址),这个指针指向了init_fnc_t *(也就是init_fnc_t类型的函数指针)。如果分析

不明白的时候就拿 char *p 这种最简单的来类比下。

怎么判断指针赋值是否正确呢?一个简单的办法就是都跳到那个地址去,看类型是否一

样,例如

char a[10];

char *p;

p = a;

这个最简单的,我们一眼就能看出来是成立的 ,前提要知道数组名就是数组首元素的

首地址; 一种简单的判断方法是跳到那个地址看看类型是否匹配,

p是 char* 类型的 ,进行一次解引用就为char 类型, a是数组首元素首地址,那么解

引用之后就到了数组首元素的位置,发现首元素的类型也是char型,所以等号成立,这

种办法判断二重指针或多重指针就方便很多了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值