C++ Pointer Help

Feb 6, 2013 at 2:16am
I'm trying to figure out the value of pointers after the following code snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int s[8] ;

int *iptr1, *iptr2 ;

int **iptr3 ;

for (int i = 0 ; i < 8 ; i++)

s[i] = 7 - i ;

iptr1 = s ;

iptr2 = &s[3] ;

iptr3 = &iptr1 ;

*iptr1 = 5 ;

*iptr2 = 11 ;

**iptr3 = 14 ;

I neeed to find what the value of iptr1, iptr2, and iptr3 are after the code is executed. I'm having trouble understanding how pointers work, so any clarity on this would be appreciated. Thanks.
Feb 6, 2013 at 10:25am
To get values of pointers you should output them on console. For example

std::cout << "iptr1 = " << iptr1 << ", iptr2 = " << iptr2 << ", iptr3 = " << iptr3 << std::endl;

Or as

printf( "iptr1 = %p, iptr2 = %p, iptr3 = %p\n", iptr1, iptr2, iptr3 );

As for array s[] then after the seria of assignments it contains

14, 6, 5, 11, 3, 2, 1, 0

You can check this yourself.

So you question is unclear. Why can not you do it yourself, can you?!
Topic archived. No new replies allowed.