Find the answer to your Linux question:
Results 1 to 5 of 5
Code: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int *index_ptr, *index_strt; index_ptr = (int *)malloc(2 * sizeof(int)); index_strt = index_ptr; srand(time(0)); *index_ptr = (char) (rand() % 128); printf("%x ...
  1. #1
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162

    Strange pointer behavior?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
            int *index_ptr, *index_strt;
    
            index_ptr = (int *)malloc(2 * sizeof(int));
            index_strt = index_ptr;
    
            srand(time(0));
    
            *index_ptr = (char) (rand() % 128);
    
            printf("%x %x\n", &index_ptr, *index_ptr);
    
            index_ptr++;
    
            *index_ptr = (char) (rand() % 128);
    
            printf("%x %x\n", &index_ptr, *index_ptr);
    
            index_ptr--;
    
            printf("%x %x\n", &index_ptr, *index_ptr);
    
            free(index_strt);
    
            return 0;
    }
    Output:

    Code:
    bfda02fc 6a
    bfda02fc 61
    bfda02fc 6a
    Anyway, my question is pretty simple. It is obvious that incrementing and decrementing the index_ptr pointer variable does advance and reverse the position because the value changes, but for some reason, the address given by &index_ptr does not. Perhaps even more confusing, is even though the address is seemingly staying the same, if I used index_ptr for "free()" it would complain because of course the address advanced.

    Now, the basic question given all of this... How do I print the address of the next bytes in the allocated block if &index_ptr remains unchanged?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Ok, here's the deal.

    You have a pointer. And you have an array of ints to which the pointer is pointing.

    When you increment the pointer, you change it, but you don't move it. It stays where it is.

    The pointer does not, itself, change location. The array doesn't change location either.

    But the pointer itself changes, even though it doesn't move, just as the array changes, even though it doesn't move.

    The content of the pointer, and the content of the array change. But the location of each stays the same.

    Let's modify your program a little:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
            int *index_ptr, *index_strt;
    
            index_ptr = (int *)malloc(2 * sizeof(int));
            index_strt = index_ptr;
    
            srand(time(0));
    
            *index_ptr = (char) (rand() % 128);
    
            printf("%x %x %x\n", &index_ptr, index_ptr, *index_ptr);
    
            index_ptr++;
    
            *index_ptr = (char) (rand() % 128);
    
            printf("%x %x %x\n", &index_ptr, index_ptr, *index_ptr);
    
            index_ptr--;
    
            printf("%x %x %x\n", &index_ptr, index_ptr, *index_ptr);
    
            free(index_strt);
    
            return 0;
    }
    When I ran it, I got this:
    Code:
    bfa9dffc 804a008 f
    bfa9dffc 804a00c 69
    bfa9dffc 804a008 f
    The middle column is, I think what you were looking for.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Yeah, I was trying to use the & pointer operator to keep track of the original pointer, but it seems easier to do with just another pointer variable or a struct ( keep the original pointer and store an offset to the current position ). Figured that out after a while, but I suppose now since I was misusing the & pointer operator, I'm not really sure what its use would be.

    What practical uses are there for it?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You want & when you want the address of a variable, even if that variable is itself a pointer. Here's an example.

    Let's say you're calling function fred(). You're passing an array of int. Let's say the fred() modifies one or more values in the array. So far, so good. But let's say that fred() also might increase the number of elements in the array. It would most likely do this with a realloc() function call. But now the array might be (and most likely is) at a different location. The pointer is at the same location as before, but fred() needs to show the caller that the array is elsewhere, so it wants to change the content of the pointer. If the caller passes not the pointer, but the address of the pointer, then fred() can replace that pointer with the pointer to the enlarged array. It can only do this if it knows where that pointer is.

    Hope this is clear.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Yeah, that makes more sense now. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...