Thread: Help with incrementing pointers in while loop

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    4

    Help with incrementing pointers in while loop

    Code:
    int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int *p = &num[0], *q = &num[9], temp;
    
       while ( p < q) {
         temp = *p;
         *p++ = *q;
         *q-- = temp;
      }
    I am confused about the increment of *p and *q in the while loop. Can someone explain how it works? Thank you.

    (this is my first post and after just joining. I apologize if anything is wrong in the post.)

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    p and q have been declared as int pointers, so when they are incremented or decremented, they move one int space forward or one int space backward. In an example:
    Code:
    int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    char word[6] = {'h', 'e', 'l', 'l', 'o'};
    int *x = &num[3];
    char *y = &word[3];
    On my system, the size of an int is 4 bytes while the size of a char is 1 byte. This means that when we do ++x, x moves forward 4 bytes. When we do --x, x moves backward 4 bytes. When we do ++y, y moves forward 1 byte. When we do --y, y moves backward 1 byte. When you then try to dereference these variables, they will try to read the data that they are then assigned to.

    Dereferencing though I think is part of your problem. When you initialize a pointer, you can do it in all of these ways:
    Code:
    int* q = &num[0];
    int * q = &num[0];
    int *q = &num[0];
    These all mean the exact same things. They mean that q has been declared a pointer. However, when you want to read what q has stored at the location it is pointing to, you would use the expression *q. As I had shows in the incrementing examples, if you want to just acess the variable q, and not what it is pointing to, you would use q. When you use *q--, you are deincrementing the value that q is pointing to, and not q itself.

    Using my above code with x and y:
    x is a pointer that points to num[3].
    x++ makes x point to num[4].
    *x is NOT a pointer. It is the value 4.
    *x++ makes num[3] equal to 5. x still points to num[3].
    Last edited by jack jordan; 12-13-2017 at 06:28 PM.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    4
    Assume the code is right because it's from my professor, what would the output of the array look like then? I understand how the pointers work in general. I just don't understand how the while loop works. You can use any numbers you'll like, I'm just trying to figure out how the temp, *p, and *q change.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    *x++ makes num[3] equal to 5. x still points to num[3].
    It seems I was wrong about this. I had assumed that x would get dereferenced first, but it seems that C's order of operations work differently than I thought they did.

    I believe that this is the same program you wrote, except when written this way it is easier to see what is happening (and with the added print):
    Code:
    #include <stdio.h>
    
    int main(void) {
    
        int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int *p = &num[0];
        int *q = &num[9];
        int temp;
    
        while (p < q) {
            temp = *p;
            *p = *q;
            p++;
            *p;
            *q = temp;
            q--;
            *q;
            printf("p = %p\nq = %p\ntemp = %d\n*p = %d\n*q = %d\n", p, q, temp, *p, *q);
            printf("%s", "num values: ");
            for (int i = 0; i < 10; ++i) {
                printf("%3d", num[i]);
            }
            puts("\n");
        }
    }
    To simplify it, you could just delete the dereferencing statements. It would make more sense then. C's order of operations basically makes them do nothing when combined with a post increment operator.
    Last edited by jack jordan; 12-13-2017 at 07:57 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    4
    OK! this actually makes alot more sense. So the increments and decrements were simply "simplified"(ironically) intoo the rest of the statements. Thank you so much!

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    4
    Thank you so much for including line 18 as well! Really clears things up!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jack jordan
    It seems I was wrong about this. I had assumed that x would get dereferenced first, but it seems that C's order of operations work differently than I thought they did.
    Yeah, *p++ is equivalent to *(p++) rather than (*p)++, hence if we want the latter parentheses should be used.

    Quote Originally Posted by jack jordan
    I believe that this is the same program you wrote
    (...)
    To simplify it, you could just delete the dereferencing statements. It would make more sense then. C's order of operations basically makes them do nothing when combined with a post increment operator.
    No, they are essential when combined with the post-increment; as standalone statements they have no net effect. Therefore, you are mistaken about including the "dereferencing statements" in the first place. This:
    Code:
    temp = *p;
    *p++ = *q;
    *q-- = temp;
    is functionally equivalent to:
    Code:
    temp = *p;
    *p = *q;
    p++;
    *q = temp;
    q--;
    not:
    Code:
    temp = *p;
    *p = *q;
    p++;
    *p;
    *q = temp;
    q--;
    *q;
    because when we examine the original code, the dereference of p and dereference of q each only happen twice.
    Last edited by laserlight; 12-13-2017 at 10:30 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non incrementing loop.
    By les2012 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2012, 11:01 PM
  2. loop not incrementing correctly
    By bobknows in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2012, 12:03 AM
  3. incrementing character strings? pointers? HELP!
    By ominub in forum C Programming
    Replies: 10
    Last Post: 05-02-2009, 09:03 PM
  4. Incrementing Pointers Along arrays
    By Red Force in forum C Programming
    Replies: 2
    Last Post: 04-16-2003, 06:45 PM
  5. incrementing pointers!?!
    By pmit2 in forum C Programming
    Replies: 2
    Last Post: 03-17-2002, 12:32 PM

Tags for this Thread