Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ typedef syntax rules

Tags:

c++

For the life of me I cannot find a good explanation of what are the rules that are used to convert a typedef to a C++ statement. The simple cases I understand. But consider this from Danny Kalev:

typedef char * pstr;
int mystrcmp(const pstr, const pstr); //wrong!

Danny Kalev then writes:

The sequence const pstr actually means char * const (a const pointer to char); not const char * (a pointer to const char.

I cannot find anywhere the rule to explain why "const pstr" would be converted to "char * const".

Thanks for any help.

like image 932
DavidS Avatar asked Dec 01 '25 05:12

DavidS


1 Answers

It's because pstr is an alias for char* and when you do const pstr is a bit like saying const (char*) and not (const char)*.

like image 87
Paul Manta Avatar answered Dec 03 '25 21:12

Paul Manta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!