如何解释复杂的 C/C++ 声明

我想很多人曾经遇到过像 int * (*fp1) (int) [10] 这样的声明;或者你无法理解的类似的东西?本文将教您解释如此复杂的 C/C++声明,包括使用打字、const 和函数指头。

你是否曾经遇到过类似。int * ( (fp1) (int) ) [10];的语句而无法理解呢?这篇文章将教你解释C/C++宣言,先易后难,从简单的c语言声明、const修饰符,typedef修饰符、函数指针,最后到”左右法则”,本文的目的是帮助理解c语言的声明,并不推荐像文中的代码一样,工作中还是要遵守c编程规范。

1. 基础

让我从一个非常简单的例子开始。考虑声明:

int n;

声明 n 为 int 类型

int *p;

声明 p 为 int 类型的指针,作者原文建议写成

int *p

而不是

int* p

这个个人认为统一就好。

还可以声明指针的指针

char **argv;

原则上,这种用法没有限制,这意味着你可以有一个指头指向指头到指头到指头,但是通常二级指针已经是比较难理解了。

考虑以下声明:

int RollNum[30][4];
int (*p)[4]=RollNum;
int *q[5];

p声明为一个指针,该指针指向一个int类型的数组,该数组大小是4。如果执行p++;p的值增加4*sizeof(int)
q声明为一个数组,数组的内容是保存指针的,什么指针?所有的指针都指向int类型的数据。

2. const修饰符

当您想要防止修改变量(这似乎是自相矛盾的)时,可以使用const关键字。在c语言声明const变量时,就需要初始化这个变量,否则在其他地方是不能赋值的。

const int n=5;
int const m=10;

两个变量是同一类型 – 常整型。这是因为C++标准规定关键字可以放置在类型或可变名称之前。就我(原文作者)个人而言,我更喜欢使用前一种风格,因为它使修饰符更清晰。

但是const和指针关联起来就相对比较难以理解了。

const int *p;
int const *q;

其种哪一个是指向一个,哪个指向一个?实际上,它们都是指向int 类型的。
p和q都指向const int,意味着均无法改变指向变量的数值。

int * const r= &n; // n has been declared as an int

这里r是一个const指针,初始化时指向了n,那么像下面的语句是非法的。

r=&m;

但是

*r=4;

是可以的。但是如果声明了下面的语句:

const int * const p=&n; // n has been declared as const int

英文:declare a const pointer to a const int,指针是const,变量也是const,都无法在运行中改变了。只能初始化进行赋值。

下面罗列了一些声明,可以参考学习:

char ** p1;                    //        pointer to       pointer to       char
const char **p2;               //        pointer to       pointer to const char
char * const * p3;             //        pointer to const pointer to       char
const char * const * p4;       //        pointer to const pointer to const char
char ** const p5;              //  const pointer to       pointer to       char
const char ** const p6;        //  const pointer to       pointer to const char
char * const * const p7;       //  const pointer to const pointer to       char
const char * const * const p8; //  const pointer to const pointer to const char

3. typedef的微妙之处

直接看代码:

typedef char * PCHAR;
PCHAR p,q;

p和q都定义为char * 类型。

下面是一些typedef的使用方法及解释:

typedef char * a;  // a is a pointer to a char

typedef a b();     // b is a function that returns
                   // a pointer to a char

typedef b *c;      // c is a pointer to a function
                   // that returns a pointer to a char

typedef c d();     // d is a function returning
                   // a pointer to a function
                   // that returns a pointer to a char

typedef d *e;      // e is a pointer to a function
                   // returning  a pointer to a
                   // function that returns a
                   // pointer to a char

e var[10];         // var is an array of 10 pointers to
                   // functions returning pointers to
                   // functions returning pointers to chars.

typedef也常见与结构体类型定义中

typedef struct tagPOINT
{
    int x;
    int y;
}POINT;

POINT p; /*有效的 C 代码 */

4. 函数指针

相对来说,函数指针较为难理解了。在原始时代的dos,近代的win32,以及x-windows中,常用于回调函数。在c++的虚函数表、STL模版, 以及 Win NT/2K/XP 系统服务等等。
函数指头的简单示例:

int (*p)(char);
void * (*a[5])(char * const, char * const);

声明p为指向一个”参数为char,返回值为int”的函数指针.

声明a为函数指针数组,数组中每个元素都指向一个

含有两个char*const指针,返回值为void*的函数

的函数指针

5 左右法则

这是一个简单的规则,允许解释任何声明。具体解释如下:

从最内侧括号开始阅读声明,向右走,然后向左走。当遇到括号时,方向应相反。一旦括号中的所有内容都解析完,就跳出来。然后继续,直到整个声明被解析。

左右规则的一个注意点:第一次开始阅读声明时,必须从标识符开始,而不是从最内在的括号开始。

举例:

int * (* (*fp1) (int) ) [10];

按以下步骤开始庖丁解牛:

从标识符开始 -------------------fp1
右边是 ) ,左右向左看,找到了*---fp1是一个指针
跳出()后遇到了(int) ------------指针指向了一个函数,函数中有一个int参数
向左看,找到了 * ---------------该函数返回指针
跳出(), 向右看,找到了 [10] ----返回的指针指向含有10个元素的数组
向左看,找到了 * ---------------数组元素都是指针
在向左看, 找到 int -------------每个数组元素指向int类型

解剖完毕
英文完整的解释:

Start from the variable name -------------------------- fp1
Nothing to right but ) so go left to find * -------------- is a pointer
Jump out of parentheses and encounter (int) --------- to a function that takes an int as argument
Go left, find * ---------------------------------------- and returns a pointer
Jump put of parentheses, go right and hit [10] -------- to an array of 10
Go left find * ----------------------------------------- pointers to
Go left again, find int -------------------------------- ints.

declare fp1 as pointer to function (int) returning pointer to array 10 of pointer to int

再来练习一个:

int *( *( *arr[5])())();
从标识符开始 -----------------arr
右边,找到[5] -----------------arr是一个数组,5个元素
向左看, 找到 * ---------------每个元素都是指针
跳出(), 向右看,找到了() ------每个指针指向函数,即数组元素为函数指针
向左看, 找到 * --------------- 所指向的函数返回指针
跳出(), 向右看,找到了() -------返回值指向函数
向左看, 找到 *  ---------------函数返回指针
继续向左看, 找到 int ----------指针指向 int类型数据.

解剖完毕
英文完整的解释:

Start from the variable name --------------------- arr
Go right, find array subscript --------------------- is an array of 5
Go left, find * ----------------------------------- pointers
Jump out of parentheses, go right to find () ------ to functions
Go left, encounter * ----------------------------- that return pointers
Jump out, go right, find () ----------------------- to functions
Go left, find * ----------------------------------- that return pointers
Continue left, find int ----------------------------- to ints.

declare arr as array 5 of pointer to function returning pointer to function returning pointer to int

罗列一些复杂的c声明以及解释供学习:

float ( * ( *b()) [] )();              // b is a function that returns a
                                       // pointer to an array of pointers
                                       // to functions returning floats.

void * ( *c) ( char, int (*)());       // c is a pointer to a function that takes
                                       // two parameters:
                                       //     a char and a pointer to a
                                       //     function that takes no
                                       //     parameters and returns
                                       //     an int
                                       // and returns a pointer to void.

void ** (*d) (int &, char **(*)(char *, char **));   // d is a pointer to a function that takes
                                       // two parameters:
                                       //     a reference to an int and a pointer
                                       //     to a function that takes two parameters:
                                       //        a pointer to a char and a pointer
                                       //        to a pointer to a char
                                       //     and returns a pointer to a pointer
                                       //     to a char
                                       // and returns a pointer to a pointer to void

float ( * ( * e[10]) (int &) ) [5];    // e is an array of 10 pointers to
                                       // functions that take a single
                                       // reference to an int as an argument
                                       // and return pointers to
                                       // an array of 5 floats.

6. 推荐阅读

欢迎关注我的公众号:

如何解释复杂的 C/C++ 声明

Original: https://www.cnblogs.com/CodeWorkerLiMing/p/14880149.html
Author: wdliming
Title: 如何解释复杂的 C/C++ 声明

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/536479/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • C#与c++对应的类型

    C#与c++对应的类型 csharp;gutter:true; C#调用C++的DLL搜集整理的所有数据类型转换方式-转载</p> <pre><cod…

    C++ 2023年5月29日
    039
  • C++ mutable的用法

    mutalbe的中文意思是”可变的,易变的”,跟constant(既C++中的const)是反义词。 在C++中,mutable也是为了突破const的限制…

    C++ 2023年5月29日
    050
  • C++教程详解

    第一篇:基础篇 简介、环境配置、基本语法、注释、数据类型、变量类型、变量作用域、常量、修饰符类型、 存储类、运算符、循环、判断、函数、数字、数组、字符串、指针、引用、日期&…

    C++ 2023年5月29日
    079
  • c++中捕捉内存泄露、异常

    cpp;gutter:false; //在Watch面板加上可以观察当前断点处最后一条异常信息:@err,hr#include "stdafx.h"</p…

    C++ 2023年5月29日
    050
  • NDK自带的c/c++库

    1.官方文档 https://developer.android.google.cn/ndk/guides/stable_apis https://developer.androi…

    C++ 2023年5月29日
    065
  • EclipseC++学习笔记-4 使用win11 wslg 启动应用

    wslg在win10下无法安装,升级win11后才可以基本按网上方法都能安装成功,但需要注意两点1、wsl –update2、如果启动程序不报错,但是不显示窗口的话 e…

    C++ 2023年5月29日
    041
  • C++多线程库的常用函数 std::this_thread::get_id()

    格式:函数 + 头文件 + 用例 + 解释说明 函数: std::this_thread::get_id() 头文件: 用例: std::thread::id master_thr…

    C++ 2023年5月29日
    044
  • Android jni c/c++线程通过CallVoidMethod调用java函数出现奔溃问题

    最近在移植网络摄像机里的p2p库到android平台,需要用到jni,最近在c线程了调用java函数的时候出现一个问题,假如在同一个线程调用java函数是没问题的,但在一个c线程了…

    C++ 2023年5月29日
    029
  • c/c++本地时间获取

    在记录程序日志时,需要记录时间。如下: 即Y为年、m为月、d为日、X为具体时分秒、A为星期、j为天数、z为其他,结果如下: 如果通过函数返回,需要这样: 其中,char tmp[6…

    C++ 2023年5月29日
    060
  • c++ string 和wstring 之间的互相转换函数

    #include <string> std::string ws2s(const std::wstring& ws) { std::string curLoca…

    C++ 2023年5月29日
    054
  • 谷歌开源替代 C++ 的编程语言:Carbon

    谷歌工程师 Chandler Carruth 近日在多伦多举办的 CppNorth 大会上宣布,正式开源谷歌内部打造的编程语言:Carbon,并称 Carbon 是 C++ 的继任…

    C++ 2023年5月29日
    074
  • C++:继承访问属性(public/protected/private)

    我不去想是否能够成功 既然选择了远方 便只顾风雨兼程 Original: https://www.cnblogs.com/adylee/p/11432895.htmlAuthor:…

    C++ 2023年5月29日
    069
  • visual studio code的c++扩展

    posted @2020-08-20 10:07 kissrule 阅读(338 ) 评论() 编辑 Original: https://www.cnblogs.com/longc…

    C++ 2023年5月29日
    099
  • [UE4]虚幻引擎的C++环境安装

    一、一般使用VS2017开发 二、需要勾选”使用C++的游戏开发” posted on2019-03-08 17:02 一粒沙 阅读(2513 ) 评论()…

    C++ 2023年5月29日
    047
  • [c++] 拷贝构造函数

    拷贝构造函数就是进行对象拷贝复制的函数。 拷贝构造函数也是一种构造函数。它用同类型的对象来初始化新创建的对象。其唯一的形参是const类型&,此函数也由系统自动调用。 拷贝…

    C++ 2023年5月29日
    049
  • C/C++中static,const,inline三种关键字的总结(参照网络)

    一、 关于staticstatic 是C++中很常用的修饰符,它被用来控制变量的存储方式和可见性,下面我将从 static 修饰符的产生原因、作用谈起,全面分析static 修饰符…

    C++ 2023年5月29日
    066
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球