+ 1

Will this give error or not ?

const int a = 7; const int *p = &a; cout<<++p;

10th Jun 2017, 9:42 AM
Shiva
Shiva - avatar
3 Respostas
+ 3
You can't perform any increments or decrements on const identifier, but the const pointer can accept the address of any other variable. const int a = 4; const int* p = &a; cout <<++p; //valid a++; //invalid not possible on const int b = 6; p=&b; //valid
10th Jun 2017, 4:13 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 4
@Shiva, it won't give any errors. Because the code is simply echoing the next address by incrementing the address stored in the pointer. If you try to change the value via dereference, than you will surely get the error i.e., *p=34 //this will give errors
10th Jun 2017, 10:22 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
0
ohk...but we are incrementing a constant variable... does this mean that we can change the address a constant pointer variable is pointing to?
10th Jun 2017, 3:33 PM
Shiva
Shiva - avatar