Is the conclusion to this code correct? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is the conclusion to this code correct?

#include <iostream> using namespace std; int main() { int a[]={1,4,9,24,5}; int *p=a; cout<<"the array is: "; for(int i=0;i<5;i++) {cout<<a[i]<<" "; }; //cout<<++*p<<endl; this increments the value at pointer first and keeps the pointer pointing at the same position //cout<<*p; output:2 2 cout<<"\n on doing *p++, it shows 1 and takes pointer to next element, which is 4: "<<*p++<<endl;//this showcases the value at pointer i.e. 1 and icrements it and points to the next element cout<<" a[0]: to show the value at a[0]remains the same: "<<a[0]<<endl; cout<<"*p shows the element which pointer points at presently: "<<*p<<endl; //this is now pointing to next element cout<<"*p-- will show the show the value of 4 and decrement the pointer to a[0] from a[1]: "<<*p--<<endl; cout<<" the pointer is now pointing at: "<<*p<<endl; cout<<"--*p this will reduce the value of a[0] by 1: "<<--*p<<endl; cout<<"where is pointer pointing now?? "<<*p<<endl; cout<<"now we show the value again and shift the pointer to a[1] by *p++: "<<*p++<<endl; cout<<"where is pointer pointing now?? "<<*p<<endl; cout<<"the array is: "; for(int i=0;i<5;i++) {cout<<a[i]<<" ";}; cout<<"\n conclusion is: prefix operators work on the present value of the pointer \n and dont shift the pointer,\n whereas the postfix show the same value \n and shift the pointer"; return 0; }

17th Sep 2020, 1:35 PM
Aayu
1 Answer
+ 2
*p++ == *(p++) changes pointer. (*p)++ changes value the pointer points to. ++*p == (++*p) changes the value of the pointer points to. *++p == *(++p) changes pointer.
17th Sep 2020, 2:38 PM
你知道規則,我也是
你知道規則,我也是 - avatar