#Having a hard time differentiating const int* u vs int const* v

53 messages · Page 1 of 1 (latest)

dusty hound
#

I feel it would be useful to memorize this, but I can't seem to fit this concept in my head. Is there are mnemonic devices you guys use?

civic citrusBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

@dusty hound

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

dusty hound
#

Any other examples, or stuff would be appreciated qwq

bitter wasp
#

const int*
and
int const*
Both are same. In C++ you can write const in two ways i.e. east const and west const.
The first one is west const and the second is east const.

dusty hound
#

Are they really the same? The example on image 2 lists some very specific use cases which apparently cause errors, which is why I'm worried about memorizing it

bitter wasp
# dusty hound Are they really the same? The example on image 2 lists some very specific use ca...

I answed based upon your question i.e. "Having a hard time differentiating const int* vs int const*".

If you are asking about image 2.
That's different.
Ok let me explain you in simple terms the difference between "pointer to const and const pointer".

The star " * " in the syntax of pointer is the main thing to look for.
If const keyword is on the left to the star then it's pointer to const and if it's it's on the right then it's const pointer.

Pointer to const - The data at the address the pointer is pointing to is const, which means you can read ( access ) it but can't mutate ( modify ) it.
Const pointer - You can read ( access ) & mutate ( modify ) the data at the location the pointer is pointing to, but you cannot change the pointer variable to point to some other location than the one which it is currently pointing to, that includes nullifying a pointer.

I hope you have understood.
I kept it simple.

dusty hound
#

[reading]

#

[reviewing]

#

[distilling into notes for further feedback]

bitter wasp
#

You can ask questions if you have.

dusty hound
#

It takes time for me to know what I don't know lol

#

Ok first things first, a pointer to const (pc) can't modify, then what is happening in this line?

#

I understand I can't mutate it like in pc[1] on the line above, but why is changing the entire thing acceptable then?

#

Or should I think of this as "this variable is pointing to a constant, you can't change what is in it, but you can replace it instead. You could think of it as like the address to your friend's house, which may or may not have been demolished and changed into a parking lot"

#

Did I say something that makes sense

lime surge
#

Neko, note that these two:

int const* ptr;
int* const ptr2;

are different

#

even though const appears on the right side in both cases

#

the position of the * symbol matters

#

the first one means:

Don't touch the thing I'm pointing to.

#

the second one means:

Don't touch me, but you may modify what I'm pointing to.

#

and ultimately you have this form:

#
int const* const ptr3;
#

it means:

Don't modify me and don't modify what I'm pointing to.

#

if you take only the part before the * star, that is:

// 1)
int
// 2)
int const
const int
#
  1. allows you to modify the int

  2. both are the same - you cannot modify the int

bitter wasp
#

That part is confusing to beginners. But don't worry.
In C++ there's something called immutable strings which are stored in data / text segment.
The way immutable strings are written is -
const char* var = "Hello";
The thing to look after is, if there's a string literal with a const char*. If a string literal is present then it's an immutable string.
Otherwise it just a pointer pointing to a data type of char which is const.

In your case, at first "pc" is pointing to "s" which is an array of char.
pc[1] = 'i' is not possible because pc is a pointer to const.
Because it is pointer to const and not const pointer, the pointer is then changed to point to "Hi" which is immutable string, which cannot be changed. And that is why your highlited line is possible.
What it is just doing is changing the pointer.

dusty hound
#

Hmm lemme clear this out real quick:

Int const* = const* int

Right?

lime surge
#

no, you may not put only const before a star

bitter wasp
lime surge
#

the syntax is like this:

DescribesWhatItPointsTo    *    DescribesThePointer    Name
#

the first part may be:

int
const int
int const

float
const float
float const

Type
const Type
Type const
// whatever you want
dusty hound
#

So wait, there’s like four possible combinations of this stuff?

#

Oh god ok lemme start making notes until I know what to ask

bitter wasp
dusty hound
#

Ok let’s do the very basic from the course material first:

const type* is like a street address, it points towards the same direction (constant), but what you may physically find there may have been torn down and replaced with something else

type* const, on the other hand, is like a light house, it always points towards the same direction (towards itself), whatever is near the lighthouse may change, but the lighthouse in itself will always stay the same?

lime surge
#

I personally don't understand this analogy

#

it doesn't make much sense to me

dusty hound
#

Ahhhh

#

I would appreciate if you had better analogies, since grasping it purely by a code and logic sense does not do the job for me

lime surge
#

Typically when you use a pointer, you want to express that:

There is a thing, say a car, somewhere else and I want to track info about it.

#

const Car* would mean

#

There is a car that I want to have information about. If I want to, I want to switch to another car but I won't ever modify anything about the car I'm tracking

#

Car* const would mean

There is a car that I want to have information about. I want to manipulate (modify) the car, but I won't ever switch to an another car

dusty hound
#

Ah that works a little better!

#

How would this analogy apply to the third example in the picture, const char* const cpc = s?

lime surge
#

Car const* const would mean

There is a car that I want to have information about. I won't ever switch to another car and I don't want to modify anything about the car

#

you still are able to read information about a car, but cannot modify it or switch to anything else

dusty hound
#

Thanks Neo and Paweł!

#

.solved

#

!solved

civic citrusBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity