Pointer Confusion | SoloLearn: Learn to code for FREE!

+1

Pointer Confusion

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

5/20/2020 7:50:27 PM

CeePlusPlus

6 Answers

New Answer

+3

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

+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.

+2

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.

+1

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

+1

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

0

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