+ 1

Pointer Confusion

Why is this the output of this code: https://code.sololearn.com/cgmfC4j01tb8/#cpp

20th May 2020, 7:50 PM
CeePlusPlus
CeePlusPlus - avatar
6 Answers
+ 3
By this a++; now a point to some uninitialized location. And giving garbage value...
20th May 2020, 7:58 PM
Jayakrishna 🇼🇳
+ 4
incrementing a pointer, points to the next memory address next to &c. dereferencing it will show some garbage value as Jayakrishna🇼🇳 mentioned . to understand better change c to array. int c [] ={5, 6}; int const *a = c; // do not use & (array) and check the output.
20th May 2020, 8:02 PM
Bahha┣
Bahha┣ - avatar
+ 3
int const* a in your programme is not a constant pointer, it's a pointer to constant data. you can't change the value, but you can change the object pointer points at.
20th May 2020, 9:28 PM
Dimas
Dimas - avatar
+ 2
it's like: const int *p: p is a pointer to int const int *const p: p is a const pointer to int that's the difference
20th May 2020, 9:30 PM
Dimas
Dimas - avatar
+ 1
Dimas ohhh that’s very interesting. Thanks for the info!
20th May 2020, 11:50 PM
CeePlusPlus
CeePlusPlus - avatar
0
Thanks Bahha and Jayakrishna :) And also, does changing memory address of a const pointer not cause an error?
20th May 2020, 9:22 PM
CeePlusPlus
CeePlusPlus - avatar