Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

const pointer – const pointer why isn't allowed

why isn’t *p = 0; and q = NULL; allowed? I know becauce Pointer is const but i don’t know why exactly NULL at q and 0 at *p?

const int i = 0;
const int *p = &i;

int j = 0;
int * const q = &j;

// *p = 0; // Fehler, *p konstant
p = NULL;

*q = 0;
// q = NULL; // Fehler, q konstant

return 0;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const int *p = &i;

Here p is non-const pointer to const int. As the pointer itself is not const, you can set it to point to something else like NULL, but what it points to is const, wo you can’t modify it through this pointer.


int * const q = &j;

Here q is const pointer to non-const int. It is initialized to point to j, and you can’t change what it points to. But you can change what ir points to though it.


const int * const r = &j;

This would be const pointer to const int. So you can’t change what it points to, and you can only read the vakue of what it points to, not change it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading