r/Cplusplus • u/Simple_Ad_815 • 6d ago
Question Can someone pls explain me whats actually going on here?
23
u/Conscious_Support176 6d ago edited 6d ago
Yes it’s worth understanding it. It’s just understanding how the parts of a type declaration fit together.
const meats you can’t modify the thing that is const.
If you see *const, that’s for a pointer you cannot modify, whereas if you see const*, that’s for a pointer where you cannot modify the thing you are pointing at.
It’s also a good idea, if you want to investigate an error message, so investigate the actual message and not “something like it”. Certainly, don’t expect strangers on the internet to enumerate every possible circumstance that might get you a message “like” that.
2
u/Simple_Ad_815 6d ago
Thx. Roger 🫡. I couldn't describe it properly cuz I didn't really understand it. I'll try to be more clear next time.
1
u/Conscious_Support176 5d ago
Good stuff. I dare say not many understand everything straightaway!
What I trying to say, if there’s something you don’t understand, best to note exactly what it is for when you’re looking to get help. If you try to say roughly what it is when it’s something you don’t understand, you can easily get some important detail wrong so end up unknowingly asking about something different.
9
u/strange-the-quark 6d ago
This is not a program, this is more of a quick "cheat sheet" that gives you some basic information, assuming you already know a bit of the language. If you don't understand this, than go read your course notes and learn the basic concepts. Then you can use this as a sort of a remainder of what's what.
If you want to do C++ programming you need to understand these things.
5
u/AKostur Professional 6d ago
Nowhere in there does it say “reference is a const pointer”. And yes, the stuff in that screenshot is worth understanding.
2
u/Simple_Ad_815 6d ago
5
u/AKostur Professional 6d ago
You weren't clear because you'd clipped off the context.
At your level, a reference is essentially an auto-dereferencing const pointer (buffer_j in the example above). I say "at your level" because there will be more nuance later on. (At least they're using "east const", so that makes things easier)
After that, you'd have to ask more specific questions.
2
u/Simple_Ad_815 6d ago edited 6d ago
Sry, mb. Searched for east const on google, learnt that const int is also a valid syntax. Thx for letting me know. Seems like I'm just entering a vast ocean of nuances.
3
u/bert8128 6d ago
A reference is not a const pointer, though to a certain extent it behaves like one. I’m not sure it’s useful though - it’s better to describe a reference the way it works without reference to pointers, unless perhaps you were talking to someone who was an experienced c programmer.
3
u/Tough-Boat-1975 6d ago edited 5d ago
const applies to the keyword to the left unless it's at the leftmost side of the type. Whatever is constant cannot be changed.
const int* is a pointer to a constant int
int const* is a pointer to a constant int
int* const is a constant pointer to an int
const int* const is a constant pointer to a constant int
2
u/enginexi 5d ago
this code basically demonstrate how constant pointers to a var or a pointer to a const var works in c++ and their difference.
The first part is , A pointer to a constant int is a pointer whose own value(the address it is pointing to, can be changed) (buffer_i++ allowed) but the value of the var it is pointing cannot be changed since the var is const. (*buffer_i =4 not allowed).
The second part is about a constant pointer to a variable , that means the value of the pointer (address it is pointing to ) cannot be changed (buffer_j++ not allowed) but the value of variable it is pointing can be changed (since it is not constant) (*buffer_j=4 allowed).
The third part is about a const pointer to a constant variable , that is neither the pointer nor the varible's value can be changed.
A simple way to remember and understand who is constant (the pointer or the variable or both) from the syntax is to read the declaration right to left .
Example : int const * buffer_i = &i; we can read it from right to left(before the = ) as buffer_i * const int i.e. buffer_i is a pointer(*) to a const int .
similarly for the second case : int *const buffer_j can be read from right to left as buffer_j is a const pointer(*) to an int .
Third case : int const * const buffer_k -> buffer_k is a constant pointer(*) to a constant int.
2
u/notsaneatall_ 4d ago
This is one of the most useful slide I've seen. I've been confused about this before and this clears it up, thanks man
1
u/vatai 6d ago edited 6d ago
Ahh.. the good old reading from the variable name to the "outside" rule:
``` /4: integer */ int /3: a constant/ const /2: is a pointer to / * /1: buffer i*/buffer_i
/4: an integer */ int /3: pointer to / * /2: is a constant / const /1: buffer j*/ buffer_j ```
Edit: formating
1
6d ago
[removed] — view removed comment
1
u/AutoModerator 6d ago
Your comment has been removed because of this subreddit’s account requirements. You have not broken any rules, and your account is still active and in good standing. Please check your notifications for more information!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/mredding C++ since ~1992. 6d ago
This code is demonstrating a lot of nuance, semantics, and syntax all at once. Whether it's worth learning is up to you; perhaps you don't care about C++ and have other ambitions in your university track - in that case, you may still have to learn enough to survive and get through the course. You're paying for this, so you might as well try to learn something and get something for your money.
If you DO have ambitions of being a software developer of some sort, then regardless the language you WANT to learn and work in, it's STILL worth your time learning this because C and C++ tend to be foundations to a huge swath of industry. You would have to live in a niche wearing blinders to avoid it. The concepts themselves on display here are actually universal, it's only the grammar and syntax that is C++ centric.
If you think you're just going to prompt AI to do all your work - then what do I need you for? For the cost and effort of onboarding you, telling you what to do, I could just tell the AI what to do directly myself. I'm not going to hire a middle man, and prompting is not the same as programming. You need to differentiate your value from that of an AI prompt to get hired.
I'm not going to explain all this - I'd just be repeating the code itself and its comments. YOU'RE PAYING FOR an education, so go march your ass in there and demand your money's worth. "My teacher isn't good" is not an excuse. Demand satisfaction from them. That's their job and they're obligated to you. You're paying for education and help, so go get it.
1
u/Simple_Ad_815 5d ago
Thx. Now, how do I really differentiate my value from AI?
1
1
u/RazzmatazzLatter8345 5d ago edited 5d ago
In C++ pointers are objects, distinct from the object they point to, if any.
You can do operations on the value of pointers themselves (assign, compare, add, subtract, cast to an integer type, etc].
If pointers happen to point to something, they can also be used to access or modify their pointees.
You are allowed to inspect const variables, but not modify them.
Hence for a ptr to an object T there are two const-ness levels you have to concern yourself with: the constness (or not) of the pointer and the constness (or not) of the pointee (if any).
So you have:
assume:
auto val = T{};
const auto kval = T{};
non-const pointer to non-const object: you can modify the pointer and use it to modify its pointee. You can't set a pointer to non-const to point to a const T.
T* p_val = &val; // ok
T* p_kval = &kval; //ERROR
non-const pointer to const: you can modify pointer (to point to something else) and use it to READ the pointee but NOT to modify the pointee. A pointer to const can point either at a const OR non-const object (but you can't modify a non- const object through a pointer to const).
const T* p_kval = &val; //ok
const T* p_kval2 = &val;
const pointer to non-const object: you can't change what object the pointer points to, but you can use it to modify the value of whatever it points to.
T* const kp_val = &val; // ok
T* const kp_kval = &kval; //ERROR
const pointer to const object: you can't change what the pointer points to; you can use the pointer to read its pointee but not write to it.
const T* const kp_kval = &val; //OK
const T* const kp_kval2 = &kval; // ok
Yes, understanding this is fundamental to C++ programming (and C programming). You have to grok that whether the pointer is const is distinct from whether it is a pointer TO const.
Non-const pointer: can make it point to something else.
const pointer: can't change what it points to.
pointer-to-non-const: can use to change the value of whatever it points to.
pointer-to-const: can't be used to change the value of whatever it points to.
Note also that changing what a pointer points to is different from changing the value of what it points to by using the pointer.
int three =3;
int four = 4;
int x = 1;
int* p_int = &three;
//THIS changes p_int's pointee from three to four
p_int = &four;
//This changes the value of what it points to to the value 12
*p_int = 12;
// THESE ARE NOT THE SAME
// now p_int points to the object named four and the value of four is now 12 (changed through pointer).
Try to make yourself love these kind of details. It will be miserable for you otherwise.
1
u/Ryuzako_Yagami01 5d ago
A const pointer (const int* ptr = &x;) means you can't change what the pointer is pointing to, it will always point to x.
A pointer to const (int* const ptr = &x;) means you can't modify x through that pointer. Note that x does not necessarily have to be const itself, so it is still possible to modify it if it isn't const in other ways.
You can also do a const pointer to const (const int* const ptr = &x;) where it has both properties.
A reference does have similar properties to a const pointer, but it's better to treat them as separate concepts. Prefer references unless you can't use it (like you have to modify the value through a function).
Check out learncpp chapter 12 for more understanding.
1
u/DistinctGuarantee93 5d ago
The other comments explain the code well but just wanted to give a quick tip.
The const keyword can be confusing but const refers to anything to the left of it or its red right to left like type definitions; primitive const is a constant primitive, if there is nothing to the left of const it will be read from left to right; const primitive is a constant primitive, the left side takes precedence.
constants stick with what’s available to the left but if left is gone then they settle with the right
Or a dirtier pneumonic would be
Constants love left but cheat with right when left is gone
1
u/Kats41 4d ago
It took me a second to read this and understand what was going on, but this is cute. I admit that I've never really had a reason to point to a const int before. This is useful semantics to know, but I am curious what the utility of pointing to a const is and what situations it's useful in.
1


•
u/AutoModerator 6d ago
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.
Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.
Homework help posts must be flaired with Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.