# Thread: pointer and arrays

1. ## pointer and arrays

Ok I read the chapter again on pointers and still can't seem to grab hold of it. I understand the what pointer are and what they do in theory just can't seem to do it in real life. Here is another pointer problem we I have to show what the output would be base of the assumed values. If possible use my code to explain pointers to me I think I will understand better.

Code:
```int list[] = {3, 9, 6};             /* assume list is at address 10240 */
int n = 150;                         /* assume n is at address 10252 *
int *p = list                        /* assume n1 is at address 10256 */
printf("%d -- %d\n", &list[0], list[0]);
printf("%d -- %d\n", &list[1], list[1]);
printf("%d -- %d\n", &list[2], list[2]);
printf("%d -- %d\n", &n, n);
printf("%d -- %d -- %d\n", &p, p, *p);
p++;
printf("%d -- %d -- %d\n", &p, p, *p);
p = list;
printf("%d -- %d\n", (p + 1), *(p + 1));
printf("%d -- %d\n", (p + 2), *(p + 2));
printf("%d -- %d\n", p + 1, *p + 1);

----- My Output ---------

10540 -- 3
10541 -- 9
10542 -- 6
10252 -- 150
10256 -- 10240 -- 3
10256 -- 10244 -- 9
10257 -- 4
10258 -- 5
10257 -- 4

-----------------------------```
I'm not real confident my output is right. using this example could someone explain it to me.

thanks.

2. Code:
```10257 -- 4
10258 -- 5
10257 -- 4```
This should be:
Code:
```10257 -- 9
10258 -- 6
10257 -- 4```
(p + 1) will print the address of p[1], because you're incrementing where it points to, not what it points to, which is &p[1] or 10257
Because *(p + 1) is equivalent to p[1], it will print the contents of p[1], which is 6.
*p + 1 will print the contents of p plus 1, because * has a lower precedence than +. So it will be 3 + 1, that's why you get 4.

3. Thanks l0cke

I think I'm finally understanding. At least a little.

4. On an aside, you shouldn't be usind %d for pointers. You should be using %p to display the value of its address. See this for more.

Quzah.

Popular pages Recent additions