2011-12-13_<2>

本文详细解析了指针运算的概念及其应用,包括间接引用和函数指针的使用,通过具体实例展示了如何在C语言中进行复杂的指针操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mutiple indirection :

int a = 3; int *b = &a; int **c = &b; int ***d = &c;

Here are how the values of these pointers equate to each other:

    *d == c;// Dereferencing an (int ***) once gets you an (int **) (3 - 1 = 2)
    **d == *c == b;// Dereferencing an (int ***) twice, or an (int **) once, gets you an (int *) (3 - 2 = 1; 2 - 1 = 1)
    ***d == **c == *b == a == 3; //Dereferencing an (int ***) thrice, or an (int **) twice, or an (int *) once, gets you an int (3 - 3 = 0; 2 - 2 = 0; 1 - 1 = 0)

the & operator can be thought of as adding asterisks (increasing pointer level, as I call it), and the *, ->, and [] operators as removing asterisks (decreasing pointer level).


Function Pointer:

char *strcpy(char *dst, const char *src);

char *(*strcpy_ptr)(char *dst, const char *src); 

strcpy_ptr = strcpy;

strcpy_ptr = &strcpy;

just like in a regular function declaration, the parameter names are optional:

char *(*strcpy_ptr_noparams)(char *, const char *) = strcpy_ptr;

The type of the pointer to strcpy is char *(*)(char *, const char *); you may notice that this is the declaration from above, minus the variable name. You would use this in a cast. For example:

strcpy_ptr = (char *(*)(char *dst, const char *src))my_strcpy;

As you might expect, a pointer to a pointer to a function has two asterisks inside of the parentheses:

char *(**strcpy_ptr_ptr)(char *, const char *) = &strcpy_ptr;

We can have an array of function-pointers:

char *(*strcpies[3])(char *, const char *) = { strcpy, strcpy, strcpy };

char *(*strcpies[])(char *, const char *) = { strcpy, strcpy, strcpy }; 

strcpies[0](dst, src);


A function declaration :

char *strcpy(char *dst, const char *src);

A function pointer variable :

char *(*strcpy_ptr)(char *dst, const char *src);

  ---->the type returned by this function is char *(*)(char *, const char *) 

Because function pointer syntax is so mind-bending, most developers use typedefs to abstract them:

typedef char *(*strcpy_funcptr)(char *, const char *);

strcpy_funcptr strcpy_ptr = strcpy;

strcpy_funcptr get_strcpy_ptr(void);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值