+ 5

C++ question with pointers

//Please someone can give me a logical explanation why this code gives -1 as result?? // and what is the value of t, p1 and p2...??; it seems an ambiguous problem int t[4]={8,4,2,1}; int *p1 = t+2, *p2 =p1-1; p1++; cout<<*p1-t[p1-p2];

16th Nov 2017, 1:15 AM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar
3 Respostas
+ 4
thanks for the answers it clears me up more
16th Nov 2017, 11:07 PM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar
+ 2
When you write p1++ you are moving the pointer in memory not changing its value. Then anything can happen. You probably want (*p1) ++ If you want to know about moving pointers google Pointer Arithmetic
16th Nov 2017, 5:47 AM
1of3
1of3 - avatar