[英]Typedef equivalence for a pointer to struct
I got this implementation of a struct: 我得到了这个结构的实现:
struct NodoQ {
Etype elem;
NodoQ *sig;
};
Is this code below, 这个代码是下面的,
typedef NodoQ *PtrNodoQ;
PtrNodoQ ppio, fin;
the same as this one? 和这个一样吗?
NodoQ* ppio;
NodoQ* fin;
Is this code below,
这个代码是下面的,
typedef NodoQ *PtrNodoQ; PtrNodoQ ppio, fin;
the same as this one?
和这个一样吗?
NodoQ* ppio; NodoQ* fin;
Yes, it's resulting in the exactly same pointer types for ppio
and fin
. 是的,它产生了与
ppio
和fin
完全相同的指针类型。
As for your comment 至于你的评论
"I didn't try cause I got the second option everywhere in my code, and just didn't want to loose some time... "
“我没有尝试,因为我的代码中到处都有第二个选项,只是不想放松一些时间......”
You can easily test it: 你可以轻松测试它:
void foo(PtrNodoQ p) {
}
void bar(NodoQ* p) {
foo(p);
}
and 和
void baz() {
NodoQ n;
foo(&n);
bar(&n);
}
compile all perfectly fine, without invalid type conversion warnings or errors. 编译完全正常,没有无效的类型转换警告或错误。
Also you could have found the answer quickly in this excellent reference (emphasis mine): 你也可以在这个出色的参考文献中快速找到答案(强调我的):
The typedef-names are aliases for existing types , and are not declarations of new types .
typedef-names是现有类型的别名 , 不是新类型的声明 。 Typedef cannot be used to change the meaning of an existing type name (including a typedef-name).
Typedef不能用于更改现有类型名称的含义(包括typedef-name)。 Once declared, a typedef-name may only be redeclared to refer to the same type again.
声明后,只能重新声明typedef-name以再次引用相同的类型。 Typedef names are only in effect in the scope where they are visible: different functions or class declarations may define identically-named types with different meaning.
Typedef名称仅在它们可见的范围内有效:不同的函数或类声明可以定义具有不同含义的相同命名的类型。
In case you wanted the standardese, [dcl.typedef] states that: 如果你想要标准,[dcl.typedef]说明:
A name declared with the
typedef
specifier becomes a typedef-name .使用
typedef
说明符声明的名称将成为typedef-name 。 Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type.在其声明的范围内, typedef-name在语法上等同于关键字,并按照第8章中描述的方式命名与标识符关联的类型。因此, typedef-name是另一种类型的同义词 。 A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.
typedef-name 不会像类声明(9.1)或枚举声明那样引入新类型。 [ Example: after
[例子:之后
typedef int MILES, *KLICKSP;
the constructions
建筑
MILES distance; extern KLICKSP metricp;
are all correct declarations;
都是正确的声明; the type of
distance
isint
and that ofmetricp
is “pointer toint
.” —end example ]distance
的类型是int
,而metricp
是“指向int
指针”metricp
末端示例]
In your case, after 在你的情况下,之后
typedef NodoQ *PtrNodoQ;
The types PtrNodoQ
and Node*
are exactly the same and can be used interchangeably from there on out. PtrNodoQ
和Node*
类型完全相同,可以从那里开始互换使用。 The declarations NodoQ* ppio;
声明
NodoQ* ppio;
and PtrNodoQ ppio;
和
PtrNodoQ ppio;
are exactly equivalent. 完全相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.