What Do These C++ Declarations Mean?

  • Context: C/C++ 
  • Thread starter Thread starter peejake
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

This discussion clarifies the meanings of various C++ declarations, specifically focusing on pointer and constant qualifiers. Declarations such as "const int a" and "int const a" are equivalent, while "const int *a" indicates a pointer to a constant integer, allowing the pointer itself to change. The declaration "int *const a" signifies a constant pointer to an integer, restricting the pointer's address but permitting changes to the integer value. Lastly, "int const * a const" denotes a constant pointer to a constant integer, prohibiting changes to both the pointer and the integer it points to.

PREREQUISITES
  • Understanding of C++ syntax and semantics
  • Familiarity with pointer concepts in C++
  • Knowledge of constant qualifiers in C++
  • Basic programming experience in C++
NEXT STEPS
  • Research C++ pointer arithmetic and its implications
  • Learn about C++ references and their advantages over pointers
  • Explore the differences between const and constexpr in C++
  • Study best practices for using pointers in C++ to avoid common pitfalls
USEFUL FOR

This discussion is beneficial for beginner C++ programmers, educators teaching C++ concepts, and developers looking to deepen their understanding of pointer and constant usage in C++.

peejake
Messages
69
Reaction score
0
Hey guys,

I need a little more help here with this C++ stuff. Can anyone give me some help on how to give the meaning of these declarations:

1) const int a;
2) int const a;
3) const int *a;
4) int *const a;
5) int const * a const;

Any help would be appreciated as i am new to c++ and i am still in the learning stage

Thanks
jake:smile:
 
Technology news on Phys.org
Welcome to real programming. Real painful, if nothing else.:rolleyes:

1 & 2 are the same. I prefer 2 on purely aesthetic grounds.

3,4,5: To read pointer declarations, work right to left.

3: "a is a pointer to (const) int" the pointee is const--it can't be changed through the pointer. The pointer can be changed; it can be pointed at a new pointee. I prefer to declare as: "int const * a", it better supports the right->left reading.

4: "a is a const pointer to int"--the pointer can't be changed--i.e. it can only point to the pointee it was initialized with; pointee can be changed via the pointer. This declaration always raises the question, would you prefer a reference in this situation? Not always (sometimes you really want an address, as in hardware programming, etc), but often a reference helps you avoid bad pointer madness that runs rampant in C.

5: "a is a const pointer to a const int"--neither pointer nor pointee can change. Akin to a const reference.

Good luck.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
89
Views
7K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 45 ·
2
Replies
45
Views
5K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K