You are not logged in.

#1 2010-06-17 01:10:43

gauthma
Member
Registered: 2010-02-16
Posts: 218
Website

Incrementing a memory address

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

#2 2010-06-17 01:22:44

Odysseus
Member
Registered: 2009-02-15
Posts: 141

Re: Incrementing a memory address

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

#3 2010-06-17 01:34:44

gauthma
Member
Registered: 2010-02-16
Posts: 218
Website

Re: Incrementing a memory address

Odysseus wrote:

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

#4 2010-06-17 01:38:32

Odysseus
Member
Registered: 2009-02-15
Posts: 141

Re: Incrementing a memory address

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

#5 2010-06-17 02:26:42

sand_man
Member
From: Australia
Registered: 2008-06-10
Posts: 2,164

Re: Incrementing a memory address

How I miss C pointers smile


neutral

Offline

#6 2010-06-22 12:28:14

xenobrain
Member
From: Lodi, CA
Registered: 2006-05-31
Posts: 91

Re: Incrementing a memory address

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

Board footer

Powered by FluxBB