- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图从语法角度理解 C 如何处理复杂类型定义的基 native 制。
考虑以下示例(问题末尾包含引用资料)。
typedef int (*p1d)[10];
is the proper declaration, i.e. p1d here is a pointer to an array of 10 integers just as it was under the declaration using the Array type. Note that this is different from
typedef int *p1d[10];
which would make p1d the name of an array of 10 pointers to type int.
因此,如果我考虑两个示例的运算符优先级(我将重写它们):
int *p1d[10]; // Becomes ...
int* p1d[10];
因此,从左到右阅读并使用运算符优先级,我得到:“指针类型为 int,名为 p1d,大小为 10”,这是错误的。至于其他/第一种情况:
int (*p1d)[10];
我读作“p1d 是一个指针,类型为 int,并且是一个包含 10 个这样的元素的数组”,这也是错误的。
有人可以解释确定这些 typedef 的规则吗?我也想将它们应用于函数指针,我希望这个讨论也能解释 const
转换背后的逻辑(即:指向常量数据的指针与指向变量数据的 const 指针)。
谢谢。
引用资料:
最佳答案
我的一位教授写道 this little guide to reading these kinds of declarations 。读一读,值得您花时间阅读,并希望能回答任何问题。
所有功劳归功于 Rick Ord ( 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)
关于c - 复杂数据类型的 Typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19502256/
我想“创建”一个类型“my_type”,它是一个 std_logic_vector(...),就像这个 C/VHDL 假代码: typedef std_logic_vector(CONSTANT do
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我已阅读说明,我明白它是一个函数类型别名。 typedef 或函数类型别名为函数类型提供一个名称,您可以在声明字段和返回类型时使用该名称。当将函数类型分配给变量时,typedef 会保留类型信息。 h
Vala 是否有能力做一些类似于 C 中的 typedef 或 D 中的 alias 的事情?我检查了它的零星文档,但找不到与此相关的任何内容。 最佳答案 不是 typedef 本身,但您可以扩展大多
我对这段代码有一些问题: typedef struct Product { char product_code[5]; int sells; int sells_quantit
我对书中的代码有疑问: const int SQUARE_ARRAY_SIZE = 4; const int SQUARE_INFO_SIZE = 4; typedef Square SquareAr
typedef经常像这样工作:typedef .但是函数指针的 typedef 似乎有不同的结构:typedef int (*fn)(char *, char *); - 没有类型别名,只有一个函
我被分配去处理一些代码,但我事先得到了一部分。它包括这部分代码: typedef int ElementType; struct Node; typedef struct Node *PtrToNod
我正在尝试创建通用容器包装器。 template class ContainerWrapper { public: using allocator_type = typename type::al
抱歉,标题太长了。 我在类列表中有一个 typedef: template class List { // Think of a class Iter_ with ListElem *pCu
我想制作一个 typedef,它依赖于模板参数中 typedef 的存在: struct foo { using MyType = int; }; template struct bar {
出于教育目的,我正在查看 Eigen 源代码。我注意到对于每个具体类模板 X在层次结构中,有一个 internal::traits定义。一个典型的例子可以在 Matrix.h 中找到: namespa
假设我有三个文件 //first.h typedef typename std::map MapVertexVd_t; //the graph class and ... //other useful
如果我需要 Swift 中的自定义类型,我可以 typedef,我该怎么做? (类似于闭包语法 typedef) 最佳答案 关键字typealias用于代替typedef: typealias Cus
由于标题可能看起来很困惑,让我举个例子: typedef bool foo[2]; typedef foo bar[4]; bar what_am_i; 所以,我认为 what_am_i 是 [4][
我想声明一个 typedef,像这样: 指向另一个 typedef 的任何数组的指针。 例如: 类型定义 1: typedef struct { int a; }structA_t1; 类型定
要将 my_int 声明为 int 的类型别名,我们可以这样写: typedef int my_int; // (1) 奇怪的是,以下内容似乎也定义了 int 别名: int typedef my
如何在其他 typedef struct 中声明 typedef struct? typedef struct { char* type; char* destination;
我正在尝试将 malloc'd ptr 保存在全局 ptr 中,以便我可以在另一个函数中使用它。理想情况下,我希望在全局范围内有一个更智能的数据结构,但现在我只是想让全局 ptr 工作。 在我的 lw
MachineState machine; ControlSignals theControls = machine.control_signals; //Why is this giving an
我是一名优秀的程序员,十分优秀!