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 ...
- 07-30-2008 #1
Strange pointer behavior?
Output: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; }
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.Code:bfda02fc 6a bfda02fc 61 bfda02fc 6a
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?
- 07-30-2008 #2
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:
When I ran it, I got this: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; }
The middle column is, I think what you were looking for.Code:bfa9dffc 804a008 f bfa9dffc 804a00c 69 bfa9dffc 804a008 f
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 07-30-2008 #3
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?
- 07-31-2008 #4
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.
- 07-31-2008 #5
Yeah, that makes more sense now. Thank you.


Reply With Quote