Can any one explain what is happening here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can any one explain what is happening here?

#include <iostream> using namespace std; int main() { int *p; (*p)++; cout<<sizeof(p)<<" "<<sizeof(*p); return 0; } // Output : No output

21st Apr 2019, 9:15 AM
Sp Maurya
Sp Maurya - avatar
2 Answers
+ 3
Your code seems to miss some lines. I amended it and wrote some comments. Hope that helps to understand it. #include <iostream> using namespace std; int main() { int a = 0; //declare & initialize a to 0 int *p; // declare a pointer p = &a; //initialize pointer to point to address of a (*p)++; // dereference pointer with (*p) so it accesses the value of a cout<<sizeof(p)<<" "<<sizeof(*p)<<" "<<*p<<" "<<p; return 0; }
21st Apr 2019, 9:32 AM
Thoq!
Thoq! - avatar
+ 1
~ swim ~ Yes you are right... #include <iostream> using namespace std; int main() { int *p; p = new int; (*p)++; cout<<sizeof(p)<<" "<<sizeof(*p); delete p; return 0; } // Output : 8 4 Now its working fine...
21st Apr 2019, 9:37 AM
Sp Maurya
Sp Maurya - avatar