How are pointers really used?

1.What can we do with pointers?

Pointers are the only way that you can see and undertand any data that is great than a few bytes in size.

  • Refer to new memory reserved during program execution

  • Refer and share large data structures without making a copy of the structures

  • To specify relationship among data-linked lists, trees, graphs. ect.

To see more, you can visit How are pointers really used?

2. How many kinds of pointer are there in C++?

注意:优先级 () > [] >*. Click here to see more.

  • point to data
    int* ; double* ;
    int (*p) [n], 指针数组 ; int* p[n] 数组指针;
    int* p = new int ; double* p = new double; (程序员必须自己释放内存 delete p, click here to see video)

  • point to function
    如果在当程序中定义了一个函数,那么在编译时系统就会为这段代码分配一段存储空间,这段存储空间的首地址称为这个函数的地址。 函数名就是这个首地址。在用函数指针变量(简称函数指针)调用函数时,只需要将函数的首地址付给指针变量,注意指向函数的指针变量没有++和–的运算。 函数指针的定义:
    函数返回值类型 (*指针变量名)(函数参数列表)
    int (*p) ( int, int); int (*p) (int*, int*)
    function pointer
    Click here to see more about pointers in C++.

3. Pointers in parameter list of function

(1)函数的声明、定义和调用
函数的声明和定义:一般为了安全性,只在头文件进行声明,在其他文件中进行定义。 当在一个源文件中定义函数且在另一个文件中调用函数时,应该在调用函数的文件顶部声明函数。
调用函数:当程序调用函数时,程序控制权会转移给被调用的函数。被调用的函数执行已定义的任务,当函数的返回语句被执行时,或到达函数的结束括号时,会把程序控制权交还给主程序。

(2)函数参数以及一级指针、二级指针

猜你喜欢

转载自blog.csdn.net/qq_34107003/article/details/83794425