2011-12-13_<2>
2011-12-13 22:31
260 查看
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:
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:
just like in a regular function declaration, the parameter names are optional:
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:
As you might expect, a pointer to a pointer to a function has two asterisks inside of the parentheses:
We can have an array of function-pointers:
A function declaration :
A function pointer variable :
---->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:
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);
相关文章推荐
- log4j2<Appenders>常用子节点浅析
- java异常处理<2>__笔记(18)
- JavaScript高级<2>
- 黑马程序员 Java基础<十>---> IO流<2>(File类,Properties,打印流,合并流)
- HTML学习 <2>
- 算法积累<2>归并排序非递归的实现
- UICollectionView学习<2>之UICollectionViewCell的自定义
- LIN总线笔记<2>
- JUnit<2>
- <2>09月18日简单汇总
- STM32存储器 — <2>STM32存储器知识的相关应用(IAP、Bit Banding)
- Android Fragment 学习<2> 滑动TAb导航(ActionBar)
- 正则表达式<2>
- <2> ulua pblua环境搭建+测试
- Java笔记3 多线程<2>线程间通信-代码分析以及多线程常见方法的运用
- UITableView使用<2>UITableViewCell的介绍
- <C/C++ 版> 设计模式 学习之 工厂模式 <2>
- c语言深入理解<2>
- 开始学习Nodejs的API参考文档<2>
- c++内存对齐<2>