View Single Post
  #5   Spotlight this post!  
Unread 21-03-2012, 11:22
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Mentor, LRI, MN RPC
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,815
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: C++ help: Understanding pointers?

Quote:
Originally Posted by ctccromer View Post
I'm working my way through an e-book called Jumping Into C++ and the past four chapters have been on Pointers. I picked up all the stuff up til Pointers very easily, but this is just frying my brain. I'm working my way through it, but I just got to "pointers of pointers" and I'm starting to freak out >.>
Don't feel bad... I think pointers are one of the hardest thing for most beginning programmers to understand (along with proper memory allocation and management).

It helps to have an understanding of how information is physically stored in a computer. You can picture it like a tall apartment building or hotel. There are a ton of blocks for data, and each one has an address. Each of those rooms, with their unique address, then contains different people (or in the case of a computer, information). So to access a particular piece of information, you need to know where its stored - you have to know its address.

For the most part, this is taken care of for you. You create a variable, and behind the scenes the variable knows where its stored and can access that data without you needing to do anything.

When you work with a pointer, however, you aren't actually storing data. You're storing that address. So you can think of a pointer as a business card sitting in a Rolodex - it isn't the data you want, but it tells you where that data is and how to access it.

For pointer usage, think about how you call functions in C++. If you send a function a couple of variables, that function uses a copy of those variables. Any changes to those variables in the function is not reflected in the caller. With pointers, however, you can pass the pointer to the data, so your function can make updates to the data and have that immediately reflected in the caller. Using pointers this way is called "pass by reference", where as the "normal" way of just passing a variable to a function is called "pass by value". Try it out - create a small program with a function in it that simply adds one to the value passed in. If you just pass in an integer, you'll see that after the function is finished, your initial value hasn't changed in the caller. If you pass in a pointer to the integer, you'll see that the value has actually changed.

Pointers can also be useful when dealing with lists, multidimensional arrays, or tree structures, especially when dealing with searching or sorting... but it's been too long since I've really had to use them for me to be any good describing them to you (I've been doing nothing but Java for about 6 years now).