I am reading C++ Primer Plus recently, and I feel that the chapter of functions and pointers is more difficult. Take notes to enhance your understanding.
From C++ Primer Plus: Chapter 7 Function:C++ Programming Modules
1. How to declare function pointer?
Similar to the function prototype: you need to declare the pointer pointing to the return value and parameter list of the function
<br>
double pam(int); //参数为int 类型,返回值为double 类型的函数 double (*pf);(int) //指向参数为int类型,返回值为double 类型的指针 pf = pam; //函数名代表了函数的地址 double x = pam(4); //函数名调用 double x = (*pf)(4); //指针调用 double x = pf(4); //C++也允许将指针名当作函数名使用
2. C++ 11 automatic typing Inference
<br>
const double * f1(const double *, int); const double * (*p1)(const double *, int); //p1 poitns to f1 auto p2 = f1; //C++11 automatic type deduction,p2 points to f1 as well
3. Use pointer name as function name
<br>
//前面函数为double *类型,cout第一部分返回double指针,第二部分返回double指针指向的值 cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl; //和上面的cout一样只不过是使用函数指针名来调用函数 cout<<p2(av,3)<<":"<<*p2(av,3)<<endl;
4 . Function pointer array
<br>
const double *(*pa[3]) (const double *,int) = {f1,f2,f3}; //创建函数指针数组 //通过指针调用函数,得到返回的指针 const double *px = pa[0](av,3); //call by pointer as if it were a function name const double *py = (*pa[0])(av,3); //正常调用 //得到函数返回指针指向的值 double x = *pa[0](av,3); double x = *(*pa[0])(av,3);
5. Pointer to pointer array
The difference between pointer array and array pointer
<br>
*pd[3] //an array of 3 pointers (*pd)[3] //a pointer to an array of three elements
Pointer to array
<br>
<br>
1 auto pc = &pa; //&pa is the entire array The address, pa is the first address of the first element of the array
2
3 const double * (*(*pd)[3])(const double *, int ) = &pa; / /Equivalent to the first
4
5 **&pa = *pa = pa[0]
Code:
<br>
//arfupt.cpp -- an array of function pointers #include<iostream> //various notations,same signatures const double *f1(const double ar[],int n); const double *f2(const double [],int); const double *f3(const double *,int); int main() { using namespace std; double av[3] = {1112.3,1542.6,2227.9}; //pointer to a function const double *(*p1)(const double *,int) = f1; auto p2 = f2;//C++ 11 utomatic type deduction //pre-C++11 can use the following code instead //const double *(*p2)(const double *,int) = f2; cout<<"Using pointers to functions:\n"; cout<<"Address Value\n"; cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl; cout<<p2(av,3)<<":"<<*p2(av,3)<<endl; //pa an array of pointers //auto doesn't work with list initialization const double *(*pa[3])(const double *,int) = {f1,f2,f3}; //pb a pointer to first element of pa auto pb = pa; // pre-C++11 can use the following code instead // const double *(**pb)(const double *, int) = pa; cout<<"\nUsing an array of pointers to functions:\n"; cout<<"Address Value\n"; for(int i = 0;i < 3; i++) cout<<pa[i](av,3)<<":"<<*pa[i](av,3)<<endl; cout<<"\nUsing a pointer to a pointer to a function:\n"; cout<<"Address Value\n"; for(int i = 0;i < 3; i++) cout<<pb[i](av,3)<<":"<<*pb[i](av,3)<<endl; //what about a pointer to an array of function pointers cout<<"\nUsing pointers to an array of pointers:\n"; cout<<"Address Value\n"; //easy way to declare pc auto pc = &pa; // pre-C++11 can use the following code instead // const double *(*(*pc)[3])(const double *, int) = &pa; cout<<(*pc)[0](av,3)<<":"<<*(*pc)[0](av,3)<<endl; //hard way to declare pd const double *(*(*pd)[3])(const double *,int) = &pa; //store return value in pdb const double *pdb = (*pd)[1](av,3); cout<<pdb<<":"<<*pdb<<endl; //alternative notation cout<<(*(pd)[2])(av,3)<<":"<<*(*(*pd)[2])(av,3)<<endl; } const double * f1(const double * ar, int n) { return ar; } const double * f2(const double ar[], int n) { return ar+1; } const double * f3(const double ar[], int n) { return ar+2; }
The above is the detailed content of C++ functions and pointers. For more information, please follow other related articles on the PHP Chinese website!

在线社区中关于鼠标还是键盘是系统最重要部分的争论可能会一直持续下去。但是,当您的鼠标指针出现问题时,您必须将所有内容放在一边,并且在找到问题的永久解决方案之前,您不能休息。在本文中,我们为罕见的鼠标指针方向错误问题策划了最佳解决方案。将指针移动到右侧,它在屏幕上向左移动,反之是然?只需按照这些简单的步骤。解决方法——如果这是您第一次遇到此问题,请按照以下步骤操作 -1. 将鼠标从系统上拆下,然后重新连接。通常,这可以解决问题。2. 如果您使用的是蓝牙鼠标,请检查鼠标的电池电量。3.尝试将鼠标与另

指针精度在需要更高精度和更好的光标定位的情况下至关重要。默认情况下,它在Windows11中处于启用状态,但您可能需要重新配置增强的指针精度以获得更好的性能。例如,您可能不希望Windows自动重新调整指针速度,而是在进行类似的鼠标移动时覆盖固定距离。什么是增强指针精度?增强的指针精度可根据鼠标移动的速度调整光标移动的距离。因此,鼠标移动越快,覆盖的距离就越大。对于那些想知道Windows增强指针精度做什么的人,它会改变鼠标灵敏度。如何在Windows11中打开或关闭增强指针精度?1.通过设置按

区别:1、表示的含义不同,“*p”表示此指针指向的内存地址中存放的内容,“p”表示一个指针变量的名字,指此指针变量所指向的内存地址。2、输出的格式不同,“*p”输出的一般是一个和指针类型一致的变量或者常量,“p”输出的是一个16进制数, 输出一个指针的地址。3、功能不同,“*p”是让程序去那个地址取出数据,“p”用于存放的是地址。

低级编程语言,如C或C++,经常使用指针来直接处理内存。它们能够实现有效的内存管理和低级数据操作。Thelow-levelcomplexitiesofmemoryadministrationareabstractedawayinPython,ahigh-levellanguage.Becauseofthis,PythonlacksexpresspointersinanequalmannerthatCorC++.Asanalternative,Pythonmakesuseofanideacompa

引用类型在Go语言中是一种特殊的数据类型,它们的值并非直接存储数据本身,而是存储数据的地址。在Go语言中,引用类型包括slices、maps、channels和指针。深入了解引用类型对于理解Go语言的内存管理和数据传递方式至关重要。本文将结合具体的代码示例,介绍Go语言中引用类型的特点和使用方法。1.切片(Slices)切片是Go语言中最常用的引用类型之一

C中const的详解及代码示例在C语言中,const关键字用于定义常量,表示该变量的值在程序执行过程中不能被修改。const关键字可以用于修饰变量、函数参数以及函数返回值。本文将对C语言中const关键字的使用进行详细解析,并提供具体的代码示例。const修饰变量当const用于修饰变量时,表示该变量为只读变量,一旦赋值就不能再修改。例如:constint

Golang是一门功能强大且高效的编程语言,可以用于开发各种应用程序和服务。在Golang中,指针是一种非常重要的概念,它可以帮助我们更灵活和高效地操作数据。指针转换是指在不同类型之间进行指针操作的过程,本文将通过具体的实例来学习Golang中指针转换的最佳实践。1.基本概念在Golang中,每个变量都有一个地址,地址就是变量在内存中的位置。

C++是一种面向对象的编程语言,它的灵活性和强大性通常为程序员提供了很大的帮助。然而,也正是因为其灵活性,编程时难以避免各种小错误。其中一个很常见的错误就是函数返回指针或引用时,不能返回局部变量或临时对象。那么该如何处理这个问题呢?本文将详细介绍相关的内容。问题的原因在C++语言中,局部变量和临时对象是在函数运行期间动态分配的。当函数结束时,这些局部变量和临


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
