# Thread: Pointers, Pointer Arithmetic and Arrays

1. ## Pointers, Pointer Arithmetic and Arrays

I'm having a bit of trouble understanding pointers and arrays. As well as pointer arithmetic. The problem is as follows:

What will be the contents of the a array after following statements are executed?

Code:
```#define N 10

int a[N] = {1 , 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N - 1], temp;

while(p < q){
temp = *p;
*p++ = *q;
*q-- = temp;
}```
I know the above code will revers the array because temp is assigned to what p is pointing to, then p is incremented to advance in the elements of the array and assigned to what q is pointing to. Then q is decremented and assigned to temp (which hold the value of what p was initially pointing to).

I had similar problem on a quiz and I got the answer incorrect. The code is as follows:
Code:
```#define N 10

int a[N] = {1 , 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N - 1];

while(p < q){
*p = *q;
*q = *p;
p++;
q--;
}```
I know p and q still get swapped in this situation. I think what tripped me up is the way p is incremented and q is decremented. Does the above code still reverse the array?

2. > What will be the contents of the a array after following statements are executed?
...
> I know the above code will revers the array because
So you know that the array will be 10,9,8,7,6,5,4,3,2,1 right?

> temp is assigned to what p is pointing to, then p is incremented
Yes.

> then p is incremented to advance in the elements of the array and assigned to what q is pointing to.
> Then q is decremented and assigned to temp (which hold the value of what p was initially pointing to).
No.
The assignments happen first, then the increment/decrement happens.
Check the position of the ++,-- in relation to the variable.

> I know p and q still get swapped in this situation
...
> I had similar problem on a quiz and I go the answer incorrect
So you don't know then.

Your 2nd example, there is no swapping.
It does however mirror the values in both halves of the array.

3. Then q is decremented and assigned to temp
temp is assigned to the address that q is pointing to, and then q is decremented.
Remember that with "post" increment or decrement, the increment/decrement expression has the old value even though the variable will ultimately be incremented.
So this:
Code:
```    while (p < q){
temp = *p;
*p++ = *q;
*q-- = temp;
}```
Is the same as this:
Code:
```    while (p < q){
temp = *p;
*p = *q;
++p;
*q = temp;
--q;
}```
In your second version, since you don't use a temp variable to hold the initial value of p, after q is assigned to p, p equals q and therefore assigning it back to q just assigns q's original value back to q.
Code:
```#include <stdio.h>

#define N 10

int main() {

int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N - 1], temp;

while (p < q){
temp = *p;
*p++ = *q;
*q-- = temp;
}

for (int i = 0; i < N; ++i)
printf("%d ", a[i]);
putchar('\n');

int a2[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p2 = &a2[0], *q2 = &a2[N - 1];

while (p2 < q2) {
*p2 = *q2;
*q2 = *p2;
p2++;
q2--;
}

for (int i = 0; i < N; ++i)
printf("%d ", a2[i]);
putchar('\n');

return 0;
}

/*
Output:
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 6 7 8 9 10
*/```

4. Originally Posted by Salem
>

> I know p and q still get swapped in this situation
...
So *p just gets assigned to *q which would be N-1 elements and *q gets assigned *p which would be the first element of the array?

Your 2nd example, there is no swapping.
It does however mirror the values in both halves of the array.

5. Originally Posted by john.c
temp is assigned to the address that q is pointing to, and then q is decremented.
Remember that with "post" increment or decrement, the increment/decrement expression has the old value even though the variable will ultimately be incremented.
So this:
Code:
```    while (p < q){
temp = *p;
*p++ = *q;
*q-- = temp;
}```
Is the same as this:
Code:
```    while (p < q){
temp = *p;
*p = *q;
++p;
*q = temp;
--q;
}```
In your second version, since you don't use a temp variable to hold the initial value of p, after q is assigned to p, p equals q and therefore assigning it back to q just assigns q's original value back to q.
Code:
```#include <stdio.h>

#define N 10

int main() {

int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N - 1], temp;

while (p < q){
temp = *p;
*p++ = *q;
*q-- = temp;
}

for (int i = 0; i < N; ++i)
printf("%d ", a[i]);
putchar('\n');

int a2[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p2 = &a2[0], *q2 = &a2[N - 1];

while (p2 < q2) {
*p2 = *q2;
*q2 = *p2;
p2++;
q2--;
}

for (int i = 0; i < N; ++i)
printf("%d ", a2[i]);
putchar('\n');

return 0;
}

/*
Output:
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 6 7 8 9 10
*/```
Now I see what Salem meant by the arrays a mirrored in both halves of the array. Thank you.

6. It takes seconds to just try it.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
#define N 10

int a[N] = {1 , 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N - 1];

while(p < q){
*p = *q;
*q = *p;
p++;
q--;
}
for ( int i = 0 ; i < N ; i++ ) {
printf("%d %d\n", i, a[i] );
}
return (0);
}

\$ gcc main.c
\$ ./a.out
0 10
1 9
2 8
3 7
4 6
5 6
6 7
7 8
8 9
9 10```

7. Originally Posted by Salem
It takes seconds to just try it.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
#define N 10

int a[N] = {1 , 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N - 1];

while(p < q){
*p = *q;
*q = *p;
p++;
q--;
}
for ( int i = 0 ; i < N ; i++ ) {
printf("%d %d\n", i, a[i] );
}
return (0);
}

\$ gcc main.c
\$ ./a.out
0 10
1 9
2 8
3 7
4 6
5 6
6 7
7 8
8 9
9 10```
Thanks!