Typedef Equivalence for a Structure Pointer

I got this implementation of the structure:

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

This code is below

 typedef NodoQ *PtrNodoQ; PtrNodoQ ppio, fin; 

same as this one?

 NodoQ* ppio; NodoQ* fin; 
+6
source share
2 answers

This code is below

 typedef NodoQ *PtrNodoQ; PtrNodoQ ppio, fin; 

same as this one?

 NodoQ* ppio; NodoQ* fin; 

Yes, this leads to exactly the same types of pointers for ppio and fin .


Regarding your comment

"I did not try because I had the second option in my code, and I just did not want to lose 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 everything perfectly, without invalid warnings or type conversion errors.


Also you could quickly find the answer in this excellent link (my attention):

Typedef names are aliases for existing types , and not declarations of new types . Typedef cannot be used to change the value of an existing type name (including typedef-name). After the declaration, the typedef name can only be updated to refer to the same type again. Typedef names are only valid in scope: different functions or class declarations can define types with the same name with different meanings.

+6
source

If you want a standard, [dcl.typedef] states that:

A name declared using the typedef qualifier becomes typedef-name. As part of its declaration, typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the method described in section 8. Thus, typedef-name is a strong character for another type. The name typedef does not introduce a new type, as the class declaration (9.1) or the enum declaration does. [Example: after

 typedef int MILES, *KLICKSP; 

constructions

 MILES distance; extern KLICKSP metricp; 

- all correct announcements; the distance type is int , and the metricp type is a "pointer to int ". -end example]

In your case after

 typedef NodoQ *PtrNodoQ; 

The types PtrNodoQ and Node* are exactly the same and can be used interchangeably from there. Ads NodoQ* ppio; and PtrNodoQ ppio; exactly equivalent.

+3
source

Source: https://habr.com/ru/post/985825/


All Articles