Pointer Confusion | SoloLearn: Learn to code for FREE!


Pointer Confusion

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

5/20/2020 7:50:27 PM


6 Answers

New Answer


By this a++; now a point to some uninitialized location. And giving garbage value...


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.


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.


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


Dimas ohhh that’s very interesting. Thanks for the info!


Thanks Bahha and Jayakrishna :) And also, does changing memory address of a const pointer not cause an error?