+ 1

What the hell is *(a2 + 2) in this code?

int a2[4] = {0,1,2,3}; /*cout << a2[0] << ", "; cout << a2[1] << ", "; cout << a2[2] << ", "; cout << a2[3] << endl; */ a2 [1] = *(a2 + 2); /*cout << a2[0] << ", "; cout << a2[1] << ", "; cout << a2[2] << ", "; cout << a2[3] << endl; */ *(a2 + 2) = a2[3]++; /*cout << a2[0] << ", "; cout << a2[1] << ", "; cout << a2[2] << ", "; cout << a2[3] << endl; */ cout << "a[1]+ a[2]= "; cout << a2[1]+a2[2];

6th Aug 2016, 4:36 AM
TylerFrost
TylerFrost - avatar
8 Réponses
+ 5
@ baba unus @chef coder It WAS a serious question. My use of hell was a simple and common use of the word to display extreme confusion. Someone answered it before you responded to decline to answer. You should not respond to rude people unless you can not be rude yourself. You are just as bad as they are. You need to improve your English because of the first 2 points I just mentioned. How do you expect to lecture someone when you may not understand what it is you're lecturing about? I did eventually answer this on my own by replacing the values, and with the test code I included with the question description, that the asterisk was acting as a dereference instead of as a mathematical operator. I asked the question in case I could not answer it myself and would have taken it down if no one responded, but I'm going to leave it up because it shows some decent test code beginners could use and so you can see my response.
6th Aug 2016, 6:19 PM
TylerFrost
TylerFrost - avatar
+ 4
a2 is a pointer on the head of the array. So *(a2+2) is equal to a2[2]
6th Aug 2016, 6:35 AM
Avispa
+ 3
* is called dereference operator it gives the value the pointer is pointing too like this int * p=& x suppose x=10 so while u write cout *p u will get 10. Similarly in pointer array *(p+i) is same as p[i] array itself is a constant pointer
6th Aug 2016, 8:44 AM
Zeeshan
0
*(a2+2)=a2[2] , both r same but written differently
6th Aug 2016, 5:27 AM
Anvesh Chanderiya
Anvesh Chanderiya - avatar
- 1
I have heard you @CHEF CODER
6th Aug 2016, 12:07 PM
Baba Yunus Abdul Yekin
Baba Yunus Abdul Yekin - avatar
- 3
I can explain it better for you but I'm not gonna do that cause what u wrote top there is not a question. Be calm especially when u need help from other people. "what the hell" sounds impolite and disrespectful on whoever is trying to help u. So get the hell yourself
6th Aug 2016, 11:12 AM
Baba Yunus Abdul Yekin
Baba Yunus Abdul Yekin - avatar
- 3
well said baba yunus and please stop sending me challenge requests.
6th Aug 2016, 12:02 PM
CHEF CODER
CHEF CODER - avatar
- 5
why did you type "what the hell"? there was no need for the abuse, idiot!
6th Aug 2016, 6:25 AM
CHEF CODER
CHEF CODER - avatar