You are not logged in.
Pages: 1
When you increment a pointer, the C language does it taking in consideration the data type the pointer points to. For instance, if you have:
int x=10;
int* p= &x;
then doing p+1 will yield p plus 4 (the length of an integer are 4 bytes).
But what if you do &p +1? I came across a similar code snippet today, and by trial and error found that this yields the address of p plus 8 (the length of a pointer are 8 bytes). In pretty much works the same way for integers: &x + 1 yields the address of x plus 4. The same holds for char (increases by 1 --- char's are 1 byte long).
To put it another way, increasing the memory address of a variable seems to give the same result as increasing a pointer that points to that variable's data type. Googling about this only brought up the usual stuff about pointer arithmetic, but nothing about the situation I've described. Is my assumption correct? Or is it there more to it than meets the eye?
Offline
They are one and the same. A memory address is a pointer and vice versa.
The type of the expression "p" is int*
The type of the expression "&x" is also int*
So it stands to reason that C will treat them no differently.
Btw, the type of the expression "&p" is int**. So yeah, its a pointer to a pointer.
Last edited by Odysseus (2010-06-17 01:27:23)
I'm the type to fling myself headlong through the magical wardrobe, and then incinerate the ornate mahogany portal behind me with a Molotov cocktail.
Offline
So it stands to reason that C will treat them no differently.
I thought as much, but as I'd never seen code like it, and could not find any relevant documentation, asking in the forum seemed a good idea.
Thanks for the quick reply!
Offline
Your welcome. When dealing with things like this I'm always reminded of dimensional analysis from physics, like you can't add meters to grams and what not.
I'm the type to fling myself headlong through the magical wardrobe, and then incinerate the ornate mahogany portal behind me with a Molotov cocktail.
Offline
How I miss C pointers
Offline
I have seen some code incrementing a pointer to an array as a way of accessing the contents of a specific array location. I don't know what if any practical advantage that has over simply deferenencing the pointer and accessing the array that way, though.
Offline
Pages: 1