A Quick Guide To Pointers

C / C++ Discuss, A Quick Guide To Pointers at Programmers Lounge forum; I was camping with my friend and (urgh) my parents and I brought C++ For Dummies, because that's my "game ...


Go Back   Gamerz Needs - For All Your Gaming Needs! > Technology Zone > Programmers Lounge > C / C++
Forgot Password? | Sign Up!

Notices

Advertisement
   

Reply
 
Bookmark this Thread Tools Display Modes
  #1  
Old 08-03-2008, 06:49 AM
RobotGymnast's Avatar
Gold Double Sided Axe+
 
Last Online: Yesterday 06:09 PM
Join Date: Oct 2006
Location: As far away from you as possible
Posts: 276
Donation Award 
Thanks: 20
Thanked 42 Times in 23 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
RobotGymnast is on a distinguished road
Points: 3,511.47
Bank: 0.00
Total Points: 3,511.47
Send a message via MSN to RobotGymnast
A Quick Guide To Pointers

I was camping with my friend and (urgh) my parents and I brought C++ For Dummies, because that's my "game ideas" notepad plus it's helpful if I'm writing code in my head when I'm bored. He borrowed it and kept complaining about the pointers part. He still complains about pointers to this day. I realized maybe pointers aren't as easy to understand as you'd think, even though I understood them easily (just the way my brain works... and because hot people understand pointers better ^^)

This code isn't all complete because I'm on a keyboard in switzerland and typing's annoying on a changed keyboard. Plus the TAB key doesn't indent.

Alright so here goes.

A pointer is essentially just a vairable containing an address. Nothing else. Whether you declare a pointer using a variable, or create it using an address yourself, it's still just a 4-Byte address (UINT32). That being said, let's look at pointer declaration and initialization.

Code:
int nPointed_at = 5;
int* pnPointed_at;
pnPointed_at = &nPointed_at;
++(*pnPointed_at);
*pnPointed_at = 10;
int nPointed_at = 5; - Simple. Declares a signed 32-bit integer with the value 5. It's convention to begin integer names with n
int* pnPointed_at; - Declares a 32-bit number that will contain the address of an int. It's convention to begin pointer variable names with p.
pnPointed_at = &nPointed_at; - Gets the address of nPointed_at, and store it in pnPointed_at. Now pnPointed_at contains the address of nPointed_at (AKA where it's stored in memory, like a street address. Addresses begin at 0x00000000 and go to 0xFFFFFFFF in a 32-bit processor)
++(*pnPointed_at); - *pnPointed_at translates to the variable at the address in pnPointed_at, or the variable pointed at by pnPointed_at, so basically you could replace *pnPointed_at by the thing at the address in there, or nPointed_at. the "++" operator increases the thing pointed at by pnPointed_at by 1. You'll notice I used brackets. I always do that in case something takes precedence where I forgot it does. In this case, it doesn't matter (I think).
*pnPointed_at = 10; - set the thing pointed at by pnPointed_at to 10 (decimal), or 0xA (hex).

Who the *bleep* cares?

That's right, I said *bleep*. *bleep*ing deal with it, you *bleep*ing *bleep* of *bleep*.

Okay, so that's how to declare and use a pointer. What's the point? (no that wasn't a bad pun.. at least, not intentionally). Pointers can be passed to functions, declared so you can edit specific memory, etc. Please consider the following (eww.. bill nye the science guy reference.. ah well.. stupid science teacher)

Code:
void change_the_value(int nValue)
{
      nValue = 6;
}

int main()
{
      int n = 5;
      change_the_value(n);
}
this declares a signed 32-bit integer with the value 5. Then it passes this integer to a function, which changes the value to 6 - theoretically. Actually the function creates a NEW variable called nValue and copies the contents of n into it. It then changes the value of nValue to 6, and then exists the function. The value passed (n) didn't actually change, because it just got copied. You could declare n globally and then try

Code:
int n;

void change_the_value()
{
      n = 6;
}

int main()
{
      n = 5;
      change_the_value();
}
I don't even know if that works in C++ (as I said earlier, I'm in a different country.. no compiler), but either way, it's not a great solution. n is only used inside main, so declaring it outside doesn't technically create problems, but it makes the code annoying to use and reread. I personally like the pointer version...

Code:
void change_the_value(int* pnValue)
{
      *pnValue = 6;
}

int main()
{
      int n = 5;
      change_the_value(&n);
}
There. In the change_the_value function, we create a new int pointer called pnValue, and copy the ADDRESS of n into it, then change the value at that address (which is n) to 6.

Now you can pass without fully copying a variable (although in this case, you would copy 4 bytes either way), keep arguments with your functions. AND (this makes it useful over the global-declaring method), you can pass different values and change each one of them, which a function which changes a certain variable can't.

Code:
void change_the_value(int* pnValue)
{
      *pnValue = 6;
}
void change_the_copied_value(int nValue)
{
      nValue = 6;
}

int main()
{
      int n = 5;
      int a = 7;
      int b = 8;
      change_the_value(&n);
      change_the_value(&a);
      change_the_copied_value(b);
}
in the end, n and a are both 6, and b remained 8, because only its copy was changed. BE SURE TO PASS ADDRESSES PROPERLY! if I said

Code:
...
change_the_value(n);
...
or

Code:
...
change_the_copied_value(&b);
...
then we would've passed n to the function (I'm pretty sure that'll actually create a compiler error), which would then think we were pointing at the address 0x00000005, and change the value there, which might be the "do you want to sign ownership of your house over to microsoft?" variable address.

in the second case, we would copy the address of b into nValue, say 0x00400000, and then change the value of the variable from that to 6. It would have no effect on that code (because we didn't change anything at the address, we just changed a value inside a variable), but would still give undesired results if the function was more complex.

Can you have pointers to pointers?

I'm glad you asked, Kevin (if that's actually your name, I'm not a stalker... even the judge ruled that I'm not).

Yup. I've actually used them. The declaration & initialization is pretty much the same.

Code:
int nValue = 5;
int* pnValue = &nValue;
int** ppnValue = &pnValue;

*pnValue = 6;
++(**ppnValue);
Declare a variable. Declare a pointer to that variable. Declare a pointer to a pointer to a variable. Add two cups flour. Bake at 375 for 20 minutes. Change the thing pointed at by the first pointer to 6. Increase the thing pointed at by the thing pointed at by the second pointer by 1. It's a lot harder to understand in text format than in code format. It's actually applicable though. For instance, you might have a pointer to an HP value that moves around every game. BUT you might have a pointer that contains the address of the pointer to the HP value. Even though your HP pointer changes, the "base pointer" (as I call it.. as do others), still contains its dynamic address, and then you could go

Code:
int* hp_pointer;
int** base_pointer = (int**)0x12345678;
hp_pointer = *base_pointer;
*hp_pointer = 999;
or

Code:
int** base_pointer = (int**)0x12345678;
**base_pointer = 999;
This assumes your base pointer is always stored at the address 0x12345678. It assumes the address 0x12345678 contains the address of the pointer to your HP. I don't know if this syntax works (no compiler), but it's something like that, because I used it for my Final Fantasy Tactics Advance DLL (the base_pointer + hp_offset was the address of the hp_pointer. Oh and it's on this site, under I think the Gameboy SP forum).

This isn't a complete reference, it's something I typed up when I got bored. To learn more, read some tutorials online, or obtain some books.

Teh Robot
__________________
Programmer in C++ and webpage stuff.. some C# and Java applets, and some DirectX & OpenGL.

My display pic is from http://www.homestarrunner.com
The Following 3 Users Say Thank You to RobotGymnast For This Useful Post:
Slugsnack (09-17-2008), wassssup34579 (08-03-2008), Xoujiro (08-22-2008)
  #2  
Old 08-22-2008, 01:22 AM
Xoujiro's Avatar
Violet Hole
 
Last Online: Today 12:05 AM
Join Date: Feb 2006
Age: 23
Posts: 305
Thanks: 12
Thanked 58 Times in 33 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 7
Xoujiro is on a distinguished road
Points: 2,785.76
Bank: 38,457.09
Total Points: 41,242.85
I think this only shows pointers in system's memory, do u mind adding how to add pointers in games, i cant find the function to get the value(where it points) of the pointer's addy, thanks in advance
__________________


  #3  
Old 08-22-2008, 05:27 AM
kaswar's Avatar
Registered Users +
 
Last Online: Today 12:03 PM
Join Date: Nov 2006
Location: Location:
Posts: 2,064
Thanks: 230
Thanked 221 Times in 145 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 11
kaswar will become famous soon enough
Points: 1,091.40
Bank: 110,398.61
Total Points: 111,490.01
Here ya go. Don't get drunk! O.O - joriannn Marked as kashin's property. (One of the four Mighty Titans led by sir SpaceCake) - kashin Merry christmas and seriously i couldnt find any other gift - ItsmYarD Merry Christmas N Happy New Years!!! - BiGbAnG to karwas. Bang - stormer320 
gotcha RPKMHFTAGUITALABPC - ROVE ure not drunk enough yet - swordmas754 
Gold - Wizxon Green - swordmas754 Green - swordmas754 
Send a message via MSN to kaswar
Quote:
Originally Posted by RobotGymnast View Post
I'm glad you asked, Kevin (if that's actually your name, I'm not a stalker... even the judge ruled that I'm not).


Holy shlt wtf bbq, if your directing that to me, My Name is Kevin. O.o


nice Tutorial I read it like a diary, Laughing once or twice at your porgramming antics. ahahahha keep up the good Work
__________________
Ha I'm back whatever
  #4  
Old 08-22-2008, 05:36 AM
1k Points Wasted
 
Last Online: Yesterday 02:08 PM
Join Date: Aug 2006
Location: over there
Posts: 2,219
Blog Entries: 6
Thanks: 48
Thanked 172 Times in 134 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog: Very random blog entries (based on time)
Rep Power: 13
cosmeo3000 will become famous soon enoughcosmeo3000 will become famous soon enough
Points: 6,003.76
Bank: 89,660.72
Total Points: 95,664.48
Black - cosmeo3000 
Send a message via MSN to cosmeo3000
Lol, it's a lot easier when you know hacking. Pointers just hold an address, and they let you access the computer's memory directly, rather than through a variable.

But can anyone explain to me the char pointers?

Are char* variables supposed to be able to make strings or something?
__________________

Dam my other one won't animate for some reason o_O
  #5  
Old 08-22-2008, 01:02 PM
Sean's Avatar
Gunbound UnderGround Level 3
 
Last Online: Today 02:00 AM
Join Date: Aug 2005
Age: 20
Posts: 4,402
Thanks: 2
Thanked 8,568 Times in 794 Posts
Nominated 33 Times in 17 Posts
Nominated TOTW/F/M Award(s): 1
Latest Blog:
Rep Power: 23
Sean has a spectacular aura aboutSean has a spectacular aura aboutSean has a spectacular aura about
Points: 41,586.56
Bank: 259,569.03
Total Points: 301,155.59
meat vomit - Ken I like people that are switched on up there. - Slugsnack I need no cells hack - huseyin No Message - goblin4u No Message - ahmedangel 
after all those bans, you still get POTM - pilotcs I love how you release only 1 new item to a hack, and get 250 more thanks. gay. =) - pilotcs Hmm. Banana. - Goku Thanks for the great MH ^^ - Adamaniac AwsOme HaCkEr - rebelife56 
I wonder if yours is longer. :) - Crumpeteer_ You Are a Cool friend so good luck and dont forget to eat my burger :)) - humam1992 A nice cold one on me! - kronikill421 best haxor - Plazma No Message - Yokerr 
GregP123 Ownz + Sean Ownz - GregP123 Cheers Sean! - chaosnite192 A beer for awesomeness! - Roguez Keep up the great works on the hacks. - Kyle No Message - jordandll 
Green - JMT420 Gold - Andrex2x Burgundy - Adamaniac Black - gogo_salem Black - shaolin786 
Dark Blue - aaronchanhongsen Gold - bmwm4 Green - hyperzuz Black - gogo_salem Gold - Roguez 
Burgundy - Torai Gold - Denipie 
Quote:
Originally Posted by cosmeo3000 View Post
Lol, it's a lot easier when you know hacking. Pointers just hold an address, and they let you access the computer's memory directly, rather than through a variable.

But can anyone explain to me the char pointers?

Are char* variables supposed to be able to make strings or something?
a char* variable is a pointer to a string. it's a definition type.
__________________


Hacks of mine that you can get if you buy premium:
  • Gunz Multi Hack (IJJI)
  • Wolfteam Multi Hack (WIS, WLS)
  • Gunbound Multi Hack (GBNA, GIS, GBEU)
  • Rakion Legit Hack (RIS, RLS, RSS)
So support GzN, buy premium, and start hacking today!
  #6  
Old 08-23-2008, 11:04 AM
RobotGymnast's Avatar
Gold Double Sided Axe+
 
Last Online: Yesterday 06:09 PM
Join Date: Oct 2006
Location: As far away from you as possible
Posts: 276
Thanks: 20
Thanked 42 Times in 23 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
RobotGymnast is on a distinguished road
Points: 3,511.47
Bank: 0.00
Total Points: 3,511.47
Send a message via MSN to RobotGymnast
LOL I knew somebody named Kevin would read this eventually >_>

also I don't know WHAT you're talking about here
Quote:
I think this only shows pointers in system's memory, do u mind adding how to add pointers in games, i cant find the function to get the value(where it points) of the pointer's addy, thanks in advance
I have no idea what you mean, but any pointers just use these concepts.

A pointer is similar to an array, the pointer shows the first element, and it keeps searching until it hits a null-terminating character
__________________
Programmer in C++ and webpage stuff.. some C# and Java applets, and some DirectX & OpenGL.

My display pic is from http://www.homestarrunner.com
  #7  
Old 09-16-2008, 06:24 PM
Wood Axe
 
Last Online: 09-19-2008 10:44 AM
Join Date: Aug 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 0
Gang Sign is an unknown quantity at this point
Points: 380.00
Bank: 0.00
Total Points: 380.00
I think your really fat...
  #8  
Old 09-16-2008, 06:28 PM
RobotGymnast's Avatar
Gold Double Sided Axe+
 
Last Online: Yesterday 06:09 PM
Join Date: Oct 2006
Location: As far away from you as possible
Posts: 276
Thanks: 20
Thanked 42 Times in 23 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
RobotGymnast is on a distinguished road
Points: 3,511.47
Bank: 0.00
Total Points: 3,511.47
Send a message via MSN to RobotGymnast
ironically, I'm quite muscular.. well for a white 15 year old who spends his days at the computer.

either way, pointless comment. Got nothing better to do?
__________________
Programmer in C++ and webpage stuff.. some C# and Java applets, and some DirectX & OpenGL.

My display pic is from http://www.homestarrunner.com
  #9  
Old 09-17-2008, 10:32 AM
X-Staff
 
Last Online: Today 05:43 PM
Join Date: Jun 2006
Location: England
Posts: 4,732
Blog Entries: 10
Thanks: 185
Thanked 1,188 Times in 638 Posts
Nominated 2 Times in 1 Post
TOTW/F/M Award(s): 0
Latest Blog: Exam Results
Rep Power: 23
Slugsnack has a spectacular aura aboutSlugsnack has a spectacular aura aboutSlugsnack has a spectacular aura about
Points: 1,003.00
Bank: 162,296.72
Total Points: 163,299.72
You need to stay healthy. :D - Kyle Marked as kashin's property. - kashin snack for slugs and worms - Ken Cheers! Youve finally got your dick to go past 1 inch. Rofl jk sluggy =P - choad Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS!!! Give Me Some SNACKS! - choad 
Gold - GodChaos Green - Slugsnack Gray - Slugsnack Gold - Slugsnack Lavender - Purple_Haze 
Dark Blue - Slugsnack Burgundy - Slugsnack Brown - Slugsnack Black - Slugsnack 
Nice tutorial, pretty good for beginners. I always feel pointers are the easiest to learn alongside ASM programming or reversing. High level programmers will often find it hard to appreciate how their structures are converted to pointers.

For example, when you declare a variable, say an INT, that is actually a 2 byte buffer. Now whenever you refer to that buffer, you refer to it by a pointer.

Also for further improvement, you could've written about arrays and how those are converted at a low level to pointers. For example, let us say we have an array with 6 members of type dword/4 bytes/long. Now let's say the first member is at address 0x0BADFOOD. Now then a pointer to it is therefore "dword ptr ds:[0x0BADFOOD]". Now then when you reference [1] of that array (second member of that array, [0] being the first), at a low level this is the original pointer plus the member number multiplied by each member's size. Therefore it is "dword ptr ds:[0x0BADFOOD+4]". If we want to iterate through the array, we can do something like this :

Code:
xor eax, eax

@@:

mov ecx, dword ptr ds:[0x0BADFOOD+4*eax]
inc eax
cmp eax, 7
jnz @b
Sorry for bringing ASM into this thread ! But I think pointers are really much easier to understand looking at them from a low level. At a high level, they are really quite hard to comprehend, eg. for the typical C programmer, pointers are considered 'complicated'

Anyway, very nice tutorial !
__________________
  #10  
Old 09-17-2008, 10:58 AM
RobotGymnast's Avatar
Gold Double Sided Axe+
 
Last Online: Yesterday 06:09 PM
Join Date: Oct 2006
Location: As far away from you as possible
Posts: 276
Thanks: 20
Thanked 42 Times in 23 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog:
Rep Power: 5
RobotGymnast is on a distinguished road
Points: 3,511.47
Bank: 0.00
Total Points: 3,511.47
Send a message via MSN to RobotGymnast
that's true, a low-level undestanding really helps with pointers, and in the end really helps with slightly higher stuff like C++. Good point
__________________
Programmer in C++ and webpage stuff.. some C# and Java applets, and some DirectX & OpenGL.

My display pic is from http://www.homestarrunner.com
Reply

Tags
c++, pointer


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump