0

I don't know what exactly is the type of a variable in C/C++ when the decalaration code is "long/complicated". For example:

int *const pr=&x;

How do I read that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Marwane
  • 333
  • 3
  • 13
  • 4
    That's a rather short declaration...? – Lundin Mar 26 '21 at 11:08
  • @Sam Varshavchik I'm not sure that duplicate is useful for anybody except language lawyers... – Lundin Mar 26 '21 at 11:11
  • I added a better post regarding `const` specifically. – Lundin Mar 26 '21 at 11:12
  • Read the type from right to left. It is a constant pointer to an integer. – ChrisMM Mar 26 '21 at 11:16
  • @ChrisMM _"Read the type from right to left. It is a constant pointer to an integer."_ That works in this case but it doesn't work in many cases: https://parrt.cs.usfca.edu/doc/how-to-read-C-declarations.html – Thomas Sablik Mar 26 '21 at 11:17
  • If you have access to a computer that can run `cdecl`, you could give cdecl this input `int *const pr;` to which it should respond `declare V as const pointer to int;` As somebody pointed out, C++ is characterized by a dysentery-like torrent of punctuation symbols. – mevets Mar 26 '21 at 11:22
  • @mevets: Given the declaration in the question, [cdecl.org](https://cdecl.org) reports “bad character '='”. So that is not helpful for somebody who does not know what the `=` character does in a declaration. – Eric Postpischil Mar 26 '21 at 12:26
  • @EricPostpischil if they don't know the difference between a declaration and an assignment, the only tool that will help them is a book. Cdecl is a wonderful thing to remember, even now after 35 years of writing C code, it helps pull apart the scribbling of the disorganized. – mevets Mar 26 '21 at 12:28
  • @mevets: `=` in a declaration just after a declarator is not an assignment. – Eric Postpischil Mar 26 '21 at 12:29
  • Maybe focus on trying to help the poor guy. – mevets Mar 26 '21 at 12:45

0 Answers0