Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ Pointers on Pointers

Pointers on Pointers

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Robert Powell   Australia  (367 posts)  Bio
Date Mon 26 Jan 2004 08:45 AM (UTC)

Amended on Mon 26 Jan 2004 08:47 AM (UTC) by Robert Powell

Message
I have some question on pointers that i dont quite understand what is happening, here is my pretend array and pointer and some declares:

int *i_pointer;
int some_array[10];
some_array[0] = 10;
some_array[1] = 22;
i_pointer = &some_array[0];

Ok stop me when im wrong.. *i_pointer is initialized to point to position 0 in the array, and as such it has a value of 10 if i was to,

*i_pointer++;

this would make i_pointer point to position 2 in the array and thus have a value of 22, then if i was to,

(*_pointer)* 2));

this would = 22 * 2 thus giving *i_pointer a value of 44, so then if i was to look into the array position 0 would have a value of 10 and position 1 would have a value of 44.
Well ok this might not be a question as such but my understanding of how this is working, what i dont know and am guessing is that when you increment the position the pointer is pointing to that it takes on the value of that position in the array. Just making sure what im doing is right and not all folly, thanks again in advance for your assistance.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Mon 26 Jan 2004 07:07 PM (UTC)
Message
When in doubt, compile.


int *i_pointer;
int some_array[10];
some_array[0] = 10;
some_array[1] = 22;
i_pointer = &some_array[0];

cout << i_pointer << endl;
cout << *i_pointer << endl;

*i_pointer++;

cout << i_pointer << endl;
cout << *i_pointer << endl;

((*i_pointer)* 2);

cout << i_pointer << endl;
cout << *i_pointer << endl;

Output:
0012FE68
10
0012FE6C
22
0012FE6C
22



*i_pointer++;
What that line does is:
Increment i_pointer. Dereference it. (And do nothing with the dereferenced value.)

((*i_pointer)* 2);
Dereference the pointer. Take the value, multiply by two. Do nothing with result.


I think your problem is that you confuse i_pointer and *i_pointer. The first means the pointer, the second, what the pointer points to.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 26 Jan 2004 07:44 PM (UTC)
Message
Ksilyan is completely right, however I'll just amplify on it with his permission. :)

You have two things here:

i_pointer - which contains (has a value of) a memory address (in Ksilyan's example the address - value - is 0012FE68).

*i_pointer - which has the value of what the pointer points to (eg. 10).


The arithmetic you are doing depends on whether you are working on the pointer or what it is pointing to. Here using brackets can help if it gets confusing.

For instance:

*i_pointer++; // <-- This increments the pointer, so it is now 0012FE6C (4 bytes on, because an int is 4 bytes). You might write this as *(i_pointer++) to be less confusing.

(*i_pointer)++; // <-- This increments what the pointer is pointing to, so 10 becomes 11. However the pointer is unchanged.


In both examples above the increment is done afterwards, so both expressions return 10. However the side-effects are different. After returning 10, the first expression is pointing to the second item in the array, and the second expression has added one to the first item in the array.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Mon 26 Jan 2004 07:51 PM (UTC)

Amended on Mon 26 Jan 2004 07:53 PM (UTC) by Nick Gammon

Message
Quote:

*i_pointer++;

this would make i_pointer point to position 2 in the array and thus have a value of 22, then if i was to,


No, it points to position 1 in the array (it started at position 0). However it (i_pointer) doesn't have the *value* 22, it has the value of another memory address (eg. 0012FE6C). However that new memory address points to the number 22.

Also, since you are doing nothing with the value being pointed to here, you may as well have just written:

i_pointer++;

Quote:

(*_pointer)* 2));

this would = 22 * 2 thus giving *i_pointer a value of 44, so then if i was to look into the array position 0 would have a value of 10 and position 1 would have a value of 44.


You have a RH bracket too many, and left off the "i", but you are half right. This returns 44, but it does not change either the pointer or the array. What you want here is:

*i_pointer *= 2;

This is an assignment statement that says "multiply the value on the left by 2 and put the result back into itself.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #4 on Tue 27 Jan 2004 04:25 AM (UTC)
Message
i did go back into my post and edit out those typo's that you picked up on, but for some reason it must not have saved them.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,311 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.