Author Topic: C++ Pointers  (Read 6782 times)

0 Members and 1 Guest are viewing this topic.

Can anybody explain to me what pointers do in layman terms? I'm on this tutorial but he sort of Wikipedialized it so it is very hard to read without stressing for a brain aneurysm.
So if anybody knows how to dumb it done for those that are a little slower on the learning curves I would greatly appreciate it ;) 

 
A pointer just points to someplace in memory.  Instead of storing the actual data it just tells you where it's located, useful for strings and dynamic stuff.
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Mongoose

  • Rikki-Tikki-Tavi
  • Global Moderator
  • 212
  • This brain for rent.
    • Minecraft
    • Steam
    • Something
What Scooby_Doo said.  A pointer is the address of a specific location in memory; you can use it to put a piece of data there and then find it later.  The actual value of a pointer is just an address value, not the data itself.  There's a related term called "dereferencing," which basically means retrieving the data that a pointer is pointing to.

 
What Scooby_Doo said.  A pointer is the address of a specific location in memory; you can use it to put a piece of data there and then find it later.  The actual value of a pointer is just an address value, not the data itself.  There's a related term called "dereferencing," which basically means retrieving the data that a pointer is pointing to.

Ah ok. So it's not actual data, but a location of where to find it sort of thing?

 
Correct.

Very rough example:
Code: [Select]
int value  = 1 ;
int *pointerValue = &value ; // pointerValue now contains the address of where value is stored at.

print pointerValue ; // some goobly gook value
print *pointerValue ; // 1

value = 2 ;
print *pointerValue ; // 2 

*pointerValue = 3 ;  // Note i'm dereferencing the pointer (i.e. i'm assigning 3 to into the memory address pointerValue is keeping)
print value ; // 3
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Spicious

  • Master Chief John-158
  • 210

 

Offline sigtau

  • 29
  • unfortunate technical art assclown
Holy crap, Scooby Doo, you just cleared up the one thing in C++ that I didn't ever, ever, EVER understand.
Who uses forum signatures anymore?

 
Watch the video: http://www.youtube.com/watch?v=i49_SNt4yfk&feature=related

Haha that video is SWEET. Wish all tutorials were like that :P
Cheers for the reference

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
another way to think of pointers is as variable variables.
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

  
Also pointers are far faster to send as parameters for a function.  Instead of copying all the data to the function call stack it just copies the pointer's value ( just a memory address). 

Also use references when possible, they're safer than pointers.



« Last Edit: October 17, 2010, 10:41:06 pm by Scooby_Doo »
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Qent

  • 29
You don't have to do anything special for that though. Big things like arrays get passed to functions as a reference automatically. ints don't need to be because they're the same size as pointers.

 

Offline Spicious

  • Master Chief John-158
  • 210
It seems a bit limiting to never use classes.

 

Offline Topgun

  • 210
Pointers are necessary if you want a function to manipulate data of another function.

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
It seems a bit limiting to never use classes.

why would you never use classes?
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 
You don't have to do anything special for that though. Big things like arrays get passed to functions as a reference automatically. ints don't need to be because they're the same size as pointers.

When did this change? 

int MyHugeArray[MAXINT] ;

somefunction (MyHugeArray) ; // makes a copy of MyHugeArray
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Spicious

  • Master Chief John-158
  • 210
why would you never use classes?
The post above implied only using things that are small enough to pass by value or arrays which pass by reference implictly.

When did this change? 

int MyHugeArray[MAXINT] ;

somefunction (MyHugeArray) ; // makes a copy of MyHugeArray
Never?
Arrays usually act like pointers.

 

Offline Sushi

  • Art Critic
  • 211
You don't have to do anything special for that though. Big things like arrays get passed to functions as a reference automatically. ints don't need to be because they're the same size as pointers.

When did this change? 

int MyHugeArray[MAXINT] ;

somefunction (MyHugeArray) ; // makes a copy of MyHugeArray

My understanding was that since arrays are pointers in c/c++, this is passing just the address of the start of the array, so this is effectively passing the array by reference. If, however, the array was wrapped in a class or struct, the whole thing would get copied.

 

Offline Mongoose

  • Rikki-Tikki-Tavi
  • Global Moderator
  • 212
  • This brain for rent.
    • Minecraft
    • Steam
    • Something
Yeah, I know that passing an array into a function passes its address, so that the function manipulates the original array.  You'd have to explicitly make a copy of the array if you didn't want the original to be changed.

 
why would you never use classes?
The post above implied only using things that are small enough to pass by value or arrays which pass by reference implictly.

When did this change? 

int MyHugeArray[MAXINT] ;

somefunction (MyHugeArray) ; // makes a copy of MyHugeArray

I thought Arrays were used if you wanted to clone something?
Never?
Arrays usually act like pointers.

 
Also use references when possible, they're safer than pointers.

This isn't entirely valid. If you're using smart pointers, you may be passing the smart pointer class, but the pointer is actually NULL!
(I know, minor, subtle point, but it's caught people before - not only that, sometimes a pointer makes more sense!)
STRONGTEA. Why can't the x86 be sane?