Doubt regarding 'Pointer to Pointer & Pointer to Array' in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Doubt regarding 'Pointer to Pointer & Pointer to Array' in C++

Please help me understand the mechanism of this program code about how it's generating the output, (-1). https://code.sololearn.com/c1Hg92zOoYE8/?ref=app

3rd Dec 2018, 4:24 PM
Learner
Learner - avatar
3 Answers
+ 10
int t[4]= {8, 4, 2, 1}; *p1 = t + 2 // p1 points to t[2] *p2 = p1 - 1 // p2 points to t[1] ++p1 // p1 points to t[3] *p1 - t[p1 - p2]; // 1-2 // -1 https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm
3rd Dec 2018, 4:54 PM
Rstar
Rstar - avatar
+ 1
Could you please tell me how is p1 - p2 = 2 (in the 4th comment). That's where exactly I was facing the problem.
3rd Dec 2018, 5:42 PM
Learner
Learner - avatar
0
Okay, got it now after a closer dive. 😅 Thanks! :)
15th May 2019, 5:46 AM
Learner
Learner - avatar