+ 1

whats wrong with this code

# include<iostream> using namespace std; int main (){ const int i = 29; const int* const ptr = &i; (*ptr)++; int j = 11; ptr = &j; cout << i; return 0; }

25th Feb 2020, 5:51 AM
Muleem Anjum
Muleem Anjum - avatar
2 Réponses
+ 3
const int* const ptr = &i This is constant pointer to the constant object. When you will assign address then you can't change the value of assigned object and also you can't "move" this pointer . I mean you can't set this pointer again to another constant object. I think there is two wrong line: (*ptr)++; ptr = &j; /* we can't move this kind of pointer*/
25th Feb 2020, 11:32 AM
electron
electron - avatar
+ 2
Constant values are read-only, so you can neither overwrite nor modify them. Attempting to increment 'i' or to reassign 'ptr' both violate this.
25th Feb 2020, 7:04 AM
Shadow
Shadow - avatar