Thread: Pointers, Pointer Arithmetic and Arrays

  1. #1
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29

    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?
    Last edited by snoopfrogg; 03-09-2019 at 10:29 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What will be the contents of the a array after following statements are executed?
    ...
    > I know the above code will revers the array because
    Your analysis is correct.
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    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 
    */
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29
    Quote Originally Posted by Salem View Post
    >

    > 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.
    Can you explain please?

  5. #5
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29
    Quote Originally Posted by john.c View Post
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29
    Quote Originally Posted by Salem View Post
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to pointers and pointer arrays
    By Dave11 in forum C Programming
    Replies: 5
    Last Post: 02-24-2014, 10:38 PM
  2. Array of pointers and pointer for arrays with argv
    By thames in forum C Programming
    Replies: 4
    Last Post: 12-02-2012, 10:24 AM
  3. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  4. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM

Tags for this Thread