复杂数据类型的 Typedef

2023-11-26

我试图从语法的角度理解 C 如何处理复杂 typedef 的底层机制。

请考虑下面的示例(问题末尾包含参考文献)。

typedef int (*p1d)[10];

是正确的声明,即 p1d 这里是指向数组的指针 10 个整数,就像使用 Array 类型声明时一样。 请注意,这不同于

typedef int *p1d[10];

这将使 p1d 成为一个由 10 个指向 int 类型的指针组成的数组的名称。

因此,如果我考虑两个示例的运算符优先级(我将重写它们):

int *p1d[10]; // Becomes ...
int* p1d[10];

因此,从左到右阅读并使用运算符优先级,我得到:“指向 int 类型的指针,名为 p1d,大小为 10”,这是错误的。至于另一种/第一种情况:

int (*p1d)[10];

我读为“p1d 是一个 int 类型的指针,并且是一个由 10 个此类元素组成的数组”,这也是错误的。

有人可以解释一下用于确定这些 typedef 的规则吗?我也想将它们应用于函数指针,我希望这次讨论也能解释背后的逻辑const强制转换(即:指向常量数据的指针与指向变量数据的常量指针)。

谢谢。

参考:


  1. C 教程:指向数组的指针:http://www.taranets.net/cgi/ts/1.37/ts.ws.pl?w=329;b=285
  2. 运算符优先级:http://www.swansontec.com/sopc.html

我的一位教授写道阅读此类声明的小指南。读一读,它值得您花时间,并希望能回答您的任何问题。

所有功劳归于瑞克·奥德(http://cseweb.ucsd.edu/~ricko/)

The "right-left" rule is a completely regular rule for deciphering C
declarations.  It can also be useful in creating them.

First, symbols.  Read

     *      as "pointer to"         - always on the left side
     []     as "array of"           - always on the right side
     ()     as "function returning"     - always on the right side

as you encounter them in the declaration.

STEP 1
------
Find the identifier.  This is your starting point.  Then say to yourself,
"identifier is."  You've started your declaration.

STEP 2
------
Look at the symbols on the right of the identifier.  If, say, you find "()"
there, then you know that this is the declaration for a function.  So you
would then have "identifier is function returning".  Or if you found a 
"[]" there, you would say "identifier is array of".  Continue right until
you run out of symbols *OR* hit a *right* parenthesis ")".  (If you hit a 
left parenthesis, that's the beginning of a () symbol, even if there
is stuff in between the parentheses.  More on that below.)

STEP 3
------
Look at the symbols to the left of the identifier.  If it is not one of our
symbols above (say, something like "int"), just say it.  Otherwise, translate
it into English using that table above.  Keep going left until you run out of
symbols *OR* hit a *left* parenthesis "(".  

Now repeat steps 2 and 3 until you've formed your declaration.  Here are some
examples:

     int *p[];

1) Find identifier.          int *p[];
                                  ^
   "p is"

2) Move right until out of symbols or right parenthesis hit.
                             int *p[];
                                   ^^
   "p is array of"

3) Can't move right anymore (out of symbols), so move left and find:
                             int *p[];
                                 ^
   "p is array of pointer to"

4) Keep going left and find:
                             int *p[];
                             ^^^
   "p is array of pointer to int". 
   (or "p is an array where each element is of type pointer to int")

Another example:

   int *(*func())();

1) Find the identifier.      int *(*func())();
                                    ^^^^
   "func is"

2) Move right.               int *(*func())();
                                        ^^
   "func is function returning"

3) Can't move right anymore because of the right parenthesis, so move left.
                             int *(*func())();
                                   ^
   "func is function returning pointer to"

4) Can't move left anymore because of the left parenthesis, so keep going
   right.                    int *(*func())();
                                           ^^
   "func is function returning pointer to function returning"

5) Can't move right anymore because we're out of symbols, so go left.
                             int *(*func())();
                                 ^
   "func is function returning pointer to function returning pointer to"

6) And finally, keep going left, because there's nothing left on the right.
                             int *(*func())();
                             ^^^
   "func is function returning pointer to function returning pointer to int".


As you can see, this rule can be quite useful.  You can also use it to
sanity check yourself while you are creating declarations, and to give
you a hint about where to put the next symbol and whether parentheses
are required.

Some declarations look much more complicated than they are due to array
sizes and argument lists in prototype form.  If you see "[3]", that's
read as "array (size 3) of...".  If you see "(char *,int)" that's read
as "function expecting (char *,int) and returning...".  Here's a fun
one:

                 int (*(*fun_one)(char *,double))[9][20];

I won't go through each of the steps to decipher this one.

Ok.  It's:

     "fun_one is pointer to function expecting (char *,double) and 
      returning pointer to array (size 9) of array (size 20) of int."

As you can see, it's not as complicated if you get rid of the array sizes
and argument lists:

     int (*(*fun_one)())[][];

You can decipher it that way, and then put in the array sizes and argument
lists later.

Some final words:

It is quite possible to make illegal declarations using this rule,
so some knowledge of what's legal in C is necessary.  For instance,
if the above had been:

     int *((*fun_one)())[][];

it would have been "fun_one is pointer to function returning array of array of
                                          ^^^^^^^^^^^^^^^^^^^^^^^^
pointer to int".  Since a function cannot return an array, but only a 
pointer to an array, that declaration is illegal.


Illegal combinations include:

     []() - cannot have an array of functions
     ()() - cannot have a function that returns a function
     ()[] - cannot have a function that returns an array

In all the above cases, you would need a set of parens to bind a *
symbol on the left between these () and [] right-side symbols in order
for the declaration to be legal.

Here are some legal and illegal examples:

int i;                  an int
int *p;                 an int pointer (ptr to an int)
int a[];                an array of ints
int f();                a function returning an int
int **pp;               a pointer to an int pointer (ptr to a ptr to an int)
int (*pa)[];            a pointer to an array of ints
int (*pf)();            a pointer to a function returning an int
int *ap[];              an array of int pointers (array of ptrs to ints)
int aa[][];             an array of arrays of ints
int af[]();             an array of functions returning an int (ILLEGAL)
int *fp();              a function returning an int pointer
int fa()[];             a function returning an array of ints (ILLEGAL)
int ff()();             a function returning a function returning an int
                                (ILLEGAL)
int ***ppp;             a pointer to a pointer to an int pointer
int (**ppa)[];          a pointer to a pointer to an array of ints
int (**ppf)();          a pointer to a pointer to a function returning an int
int *(*pap)[];          a pointer to an array of int pointers
int (*paa)[][];         a pointer to an array of arrays of ints
int (*paf)[]();         a pointer to a an array of functions returning an int
                                (ILLEGAL)
int *(*pfp)();          a pointer to a function returning an int pointer
int (*pfa)()[];         a pointer to a function returning an array of ints
                                (ILLEGAL)
int (*pff)()();         a pointer to a function returning a function
                                returning an int (ILLEGAL)
int **app[];            an array of pointers to int pointers
int (*apa[])[];         an array of pointers to arrays of ints
int (*apf[])();         an array of pointers to functions returning an int
int *aap[][];           an array of arrays of int pointers
int aaa[][][];          an array of arrays of arrays of ints
int aaf[][]();          an array of arrays of functions returning an int
                                (ILLEGAL)
int *afp[]();           an array of functions returning int pointers (ILLEGAL)
int afa[]()[];          an array of functions returning an array of ints
                                (ILLEGAL)
int aff[]()();          an array of functions returning functions
                                returning an int (ILLEGAL)
int **fpp();            a function returning a pointer to an int pointer
int (*fpa())[];         a function returning a pointer to an array of ints
int (*fpf())();         a function returning a pointer to a function
                                returning an int
int *fap()[];           a function returning an array of int pointers (ILLEGAL)
int faa()[][];          a function returning an array of arrays of ints
                                (ILLEGAL)
int faf()[]();          a function returning an array of functions
                                returning an int (ILLEGAL)
int *ffp()();           a function returning a function
                                returning an int pointer (ILLEGAL)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

复杂数据类型的 Typedef 的相关文章

  • 如何在 DataColumn.Expression 中使用 IF/ELSE 或 CASE?

    我有一个包含 1 列的表 状态 我想添加另一列名为 Action 的列 其值如下 如果 Status Yes 则 Action Go 否则 Action Stop 我使用以下代码添加到 操作 列中 但它不起作用 myDataTable Co
  • 在实体框架拦截器中向 DbScanExpression 添加内部联接

    我正在尝试使用实体框架 CommandTree 拦截器通过 DbContext 向每个查询添加过滤器 为了简单起见 我有两个表 一个称为 User 有两列 UserId 和 EmailAddress 另一个称为 TenantUser 有两列
  • 在 C# 中按元素相乘数组具有意想不到的性能

    我想找到按元素相乘两个数组的最佳方法 这是更广泛项目的一部分 其中性能而不是唯一的考虑因素 我今天开始用 C Linqpad 编写一些函数 因此它还没有以任何方式进行优化 下面代码的输出如下 Environment ProcessorCou
  • 更改 Qt OpenGL 窗口示例以使用 OpenGL 3.3

    我正在尝试更改 Qt OpenGL 示例以使用更现代的 opengl 版本 330 似乎合适 所以我做了 在 main cpp 上设置版本和配置文件 设置着色器版本 更改着色器以使用统一 它现在构建没有任何错误 但我只看到一个空白窗口 我错
  • 如何在 C# / .NET 中创建内存泄漏[重复]

    这个问题在这里已经有答案了 可能的重复 托管代码中是否可能存在内存泄漏 特别是 C 3 0 https stackoverflow com questions 6436620 is it possible to have a memory
  • EF Core 通过完全替换断开集合导航属性的更新

    使用 EF Core 5 0 我有一个 SPA 页面 可以加载Group实体及其集合Employee来自 API 的实体 var groupToUpdate await context Groups Include g gt g Emplo
  • 读取 C# 中的默认应用程序设置

    我的自定义网格控件有许多应用程序设置 在用户范围内 其中大部分是颜色设置 我有一个表单 用户可以在其中自定义这些颜色 并且我想添加一个用于恢复默认颜色设置的按钮 如何读取默认设置 例如 我有一个名为的用户设置CellBackgroundCo
  • 防止 boost::asio::io_context 在空轮询调用时停止

    此代码调用发布的句柄 boost asio io context ioc boost asio post ioc std cout lt lt lol lt lt std endl ioc poll 而这并没有 boost asio io
  • 信号处理程序有单独的堆栈吗?

    信号处理程序是否有单独的堆栈 就像每个线程都有单独的堆栈一样 这是在 Linux C 环境中 来自 Linux 手册页signal 7 http kernel org doc man pages online pages man7 sign
  • 如何在 SqlDataReader.Read() 期间从死锁异常中恢复

    我的 NET 应用程序的事件日志显示 它在从 Sql Server 读取数据时偶尔会出现死锁 这种情况通常非常罕见 因为我们已经优化了查询以避免死锁 但有时仍然会发生 过去 我们在调用ExecuteReader函数在我们的SqlComman
  • 指向特征矩阵的指针数组

    我在代码中使用 Eigen 的 MatrixXd 矩阵 在某个时刻我需要一个 3D 矩阵 由于 Eigen 没有三维矩阵类型 因为它仅针对线性代数进行了优化 因此我创建了一个 MatrixXd 类型的指针数组 Eigen MatrixXd
  • 单例模式和 std::unique_ptr

    std unique ptr唯一地控制它指向的对象 因此不使用引用计数 单例确保利用引用计数只能创建一个对象 那么会std unique ptr与单例执行相同 单例确保只有一个实例属于一种类型 A unique ptr确保只有一个智能指针到
  • Visual Studio Code:如何配置 includePath 以获得更好的 IntelliSense 结果

    我是使用 Visual Studio Code 的完全初学者 我不知道我在做什么 我已经四处搜索 也许还不够 但我找不到像我这样的人如何配置的简单解释c cpp properties json每当我单击带有绿色波浪线下划线的行旁边的黄色灯泡
  • std::forward_as_tuple 将参数传递给 2 个构造函数

    我想传递多个参数以便在函数内构造两个对象 以同样的方式std pair
  • 如何从文本文件读取整数到数组

    这就是我想做的 我对此有些不满 但我希望你能容忍我 这对我来说是一个非常新的概念 1 在我的程序中 我希望创建一个包含 50 个整数的数组来保存来自文件的数据 我的程序必须获取用户的文档文件夹的路径 2 文件的名称为 grades txt
  • C++ php 和静态库

    我创建了一个library a 其中包含 cpp 和 h 文件 其中包含很多类 嵌套类和方法 我想在 php 示例中包含这个静态库并尝试使用它 我想提一下 我是 php 新手 我已经在 test cpp 文件中测试了我的 libray a
  • 如何通过 JsonConvert.DeserializeObject 在动态 JSON 中使用 null 条件运算符

    我正在使用 Newtonsoft 反序列化已知的 JSON 对象并从中检索一些值 如果存在 关键在于对象结构可能会不断变化 因此我使用动态来遍历结构并检索值 由于对象结构不断变化 我使用 null 条件运算符来遍历 JSON 代码看起来像这
  • 如何分析组合的 python 和 c 代码

    我有一个由多个 python 脚本组成的应用程序 其中一些脚本正在调用 C 代码 该应用程序现在的运行速度比以前慢得多 因此我想对其进行分析以查看问题所在 是否有工具 软件包或只是一种分析此类应用程序的方法 有一个工具可以将 python
  • 每个数据库多个/单个 *.edmx 文件

    我有一个通过 ADO net 数据服务与数据库交互的项目 数据库很大 近 150 个具有依赖关系的表 该项目几年前开始 当时使用的是数据集 现在我们正在转向实体模型关系 由于我们添加了更多需要使用的表 该模型正在不断增长 这是管理这一切的正
  • xsi:type 属性搞乱了 C# XML 反序列化

    我使用 XSD exe 根据 XML 架构 xsd 文件 自动生成 C 对象 我正在反序列化 OpenCover 输出 但其中一个部分类未正确生成 这是导致异常的行

随机推荐

  • 安卓apk无法安装?我在哪里可以检查此日志?没有给出理由

    我点击 apk 文件 它提示安装 但很快停止 没有指示它是否已安装或问题是什么 如何查看未安装的原因 此类信息的日志在哪里 您可以通过 USB 将您的设备连接到您的机器吗 您应该能够转到提示并执行以下操作 adb install
  • 如何用Matlab构造不等宽直方图?

    我想构造一个具有不等箱 间隔 的直方图 Matlab 仅构造具有相等箱的直方图 就好像它是一个图表一样 请帮助我 非常感谢 这是一个例子 x randn 100 1 3 some random data e 10 5 3 1 1 2 3 2
  • virtualenv蛋黄问题

    yolk l给我的信息是我的 Ubuntu 10 04 上安装了 114 个软件包 使用创建新的 virtualenv 目录后 virtualenv virt env virt1 no site packages clear 我切换到那个
  • Linq-to-Entities:选择查询表达式中的日期格式

    我试图直接从 LINQ to Entities 查询表达式获取格式化的日期字符串 nonBusinessDays from ac in db AdminCalendar where ac DateTimeValue gt calendarS
  • Android Toast 通知在冰淇淋三明治中太小 [重复]

    这个问题在这里已经有答案了 有人对冰淇淋三明治中的吐司通知太小有任何问题吗 在我的应用程序中 通知在所有其他版本上看起来都很好 然而 在冰淇淋三明治中 它的尺寸却小得令人难以置信 起初我以为这可能是我的自定义主题 但禁用它后 我看到了相同的
  • 将数据库保存在外部硬盘上

    我正在使用 PostgreSQL 创建一些数据库 但由于我的计算机内存不足 我想将它们保存在外部硬盘上 我怎样才能做到这一点 您可以将数据库存储在另一个磁盘上 方法是将其指定为data directory环境 您需要在启动时指定它 它将适用
  • 在设计 JS 库时,我应该使其兼容 RequireJS/AMD 还是不兼容?

    如果我正在制作一个通用的 JavaScript 库 我应该如何处理 RequireJS 支持 据我所知 让你的代码或多或少地兼容 RequireJS 会让你的代码在没有 RequireJS 的情况下无法使用 那我为什么要这么做呢 没有 Re
  • omu.valueinjecter 深度克隆与类型不同

    我认为我缺少一个关于 valueinjecter 和 或 AutoMapper 的简单概念 但是如何将父级 dto Entity 深度克隆到 biz Entity 并包含所有子级 例如 biz person InjectFrom dto p
  • 如何创建 NodeJS 模块?

    我已阅读 NodeJS 网站上的详细信息 https nodejs org api modules html 我不明白模块是如何工作的 创建模块的最少步骤是什么 以及 npm 如何帮助我 如何创建模块 我如何使用模块 把它放在 npm 上是
  • 如何在 UIDatePicker 中仅禁用日期选择而不是时间选择

    我在我的应用程序中使用 UIDatePicker 我想仅禁用其中的日期选择 并且时间选择不应受到影响 谁能帮我这个 Set the datePickerMode to UIDatePickerModeTime
  • 使用与密钥相同的字符串加密字符串是否安全?

    在 CBC 模式下使用 AES 并使用 IV 当然 对给定密钥本身进行加密是否存在任何安全缺点 遵循以下原则 密钥是秘密的 IV 是公开的 因为这不会影响加密的安全性 然而 潜在的攻击者将会知道 因为他可以访问源代码 该字符串使用其自身作为
  • 具有相机意图和图像选择器意图的选择器

    我创建了一个选择器 用于从文件中选择图像或制作图片 我使用的代码在 Nexus 5 上运行良好 但是当我在 Samsung S5 上尝试时 选择器不显示相机图标 public Intent makePhotoIntent String ti
  • docker 互联网连接 iptables=false

    我放弃 我在 ubuntu 16 04 下使用 Docker 1 12 0 并使用 UFW 进行强化 该机器有 2 个接口 一个公共 eth0 和一个专用网络 eth1 Server Version 1 12 3 Storage Drive
  • 如何仅使用 CSS 获得悬停效果

    我已经得到了圆圈部分 我在 div 上设置了黑色的背景颜色 对于文本 我设置了 a hover 作为显示的颜色 我只是不知道如何为 div 设置 a hover 以及仅针对该周长 这是我的代码 HTML a class cirlink hr
  • 静态成员的两个实例,怎么可能?

    我有一个多线程应用程序 我在共享库中声明一个具有静态成员的类 从不同库的不同线程打印成员的地址会显示不同的结果 宣言 template
  • 指令在 ng-repeat 绑定的 内不起作用

    我有一个表 其中的行通过重复ng repeat 我正在尝试创建一个生成列的模板 td 对于每一行 tr app directive customtd function return restrict E template td positi
  • 来自 AngularJS Get for JSON 的 HTTP 状态 0

    我正在为 JSON 运行 http get 并且状态为 0 我已经下载了相同的 JSON 并且 get 在本地工作 并且在 Python 中使用请求库我可以毫无问题地获取 JSON 但是在AngularJS 不起作用 我不明白的是为什么 A
  • 在 macOS 的 SwiftUI 中更改 TextEditor 背景颜色

    我想更改 macOS 上 SwiftUI 文本编辑器的背景颜色 下面的代码 用于 iOS 是否有一个变体适用于 NSTextField 而不是 UITextView Thanks struct ContentView View init U
  • 社交网络应用程序数据库设计:如何改进此架构?

    背景 我正在为诗人和作家开发一款社交网络应用程序 让他们能够分享诗歌 收集反馈并与其他诗人交流 我很少接受过数据库设计方面的正式培训 但我一直在阅读书籍 SO 和在线数据库设计资源 试图在不过度设计的情况下确保性能和可扩展性 数据库是MyS
  • 复杂数据类型的 Typedef

    我试图从语法的角度理解 C 如何处理复杂 typedef 的底层机制 请考虑下面的示例 问题末尾包含参考文献 typedef int p1d 10 是正确的声明 即 p1d 这里是指向数组的指针 10 个整数 就像使用 Array 类型声明