gpt4 book ai didi

c++ - 指向结构的指针的 Typedef 等价

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:10 27 4
gpt4 key购买 nike

我得到了这个结构的实现:

struct NodoQ {
Etype elem;
NodoQ *sig;
};

下面是这段代码吗,

typedef NodoQ *PtrNodoQ;
PtrNodoQ ppio, fin;

和这个一样吗?

NodoQ* ppio;
NodoQ* fin;

最佳答案

Is this code below,

typedef NodoQ *PtrNodoQ;
PtrNodoQ ppio, fin;

the same as this one?

NodoQ* ppio;
NodoQ* fin;

是的,它导致 ppiofin 的指针类型完全相同。


至于你的评论

"I didn't try cause I got the second option everywhere in my code, and just didn't want to loose some time... "

你可以很容易地测试它:

void foo(PtrNodoQ p) {
}

void bar(NodoQ* p) {
foo(p);
}

void baz() {
NodoQ n;
foo(&n);
bar(&n);
}

编译一切都很好,没有无效类型转换警告或错误。


您也可以在 this excellent reference 中快速找到答案(强调我的):

The typedef-names are aliases for existing types, and are not declarations of new types. Typedef cannot be used to change the meaning of an existing type name (including a typedef-name). Once declared, a typedef-name may only be redeclared to refer to the same type again. 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.

关于c++ - 指向结构的指针的 Typedef 等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29834657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com