I still don't know pointers...

Jan 25, 2011 at 12:00am
Well I've been programming for a fair amount of time and I still don't know pointers. Does anyone have a program idea that I can make that is pointer heavy?
I really want to learn this and I know its a very powerful tool. What do you guys think?
Jan 25, 2011 at 12:02am
How about a linked list?
Jan 25, 2011 at 12:07am
If you don't use pointers or references and program in C++ you must be keeping a lot of duplicate data, no? In languages like Python or Java, all variables are actually pointers, so if you're used to one of them you should have an idea of what you can do with them. Often you'll have objects in your program that need to be known by other objects who shouldn't own them. In Python, for instance, you'd handle this by simple assignment. In C++ you keep a pointer (or reference). This way, other objects can "talk about" this one object and interact with it without the need to copy a lot of data around.
Last edited on Jan 25, 2011 at 12:08am
Jan 25, 2011 at 12:09am
Hmm sounds interesting but I've heard of it. Care to explain?
Last edited on Jan 25, 2011 at 12:09am
Jan 25, 2011 at 12:16am
If you don't use pointers or references and program in C++ you must be keeping a lot of duplicate data, no?

Well I haven't done anything that hardcore.
Jan 25, 2011 at 12:19am
http://en.wikipedia.org/wiki/Linked_list
Basically you have a series of "nodes," structures (or classes) that contain some data and a pointer to the next node in the list.
Jan 25, 2011 at 12:23am
I'll give it a shot.
Jan 25, 2011 at 3:55pm
There is no reason to over-complicate a simple thing like pointers. If you take your car in for service do you:

1. Make a complete, detailed COPY of your car and give it to the repair guy, let him fix it and then incorporate the repairs you see in the copy back into your original car or ...

2. POINT at your actual car sitting in the parking lot and say "Fix it"?

Same thing with pointers/references in programming. It is very inefficient to copy a large, complex data structure and send it to a module/function for processing and then turn around and overwrite the original data with the newly processed version. Instead, you just pass along a pointer to the location in memory where your data structure resides to the function and the function operates on the actual data 'in-place'. This is called pass-by-reference.

So why not ALWAYS use pointers?

1. Its added complexity doesn't make sense for small data structures.
2. There's an element of safety working on a copy rather than the original.
3. You better be damn sure that no other part of your program is accidentally pointing at your car with the function to paint it hot pink with purple polka-dots!

Providing a function with an actual copy of your data is called pass-by-value.
Jan 25, 2011 at 5:03pm
I think you already know the concept of pointer
it is just a variable point to the address of different data type
but this is not enough for you to handle the power of pointer

pick a book of data structure and make some simple data structure
like stack, queue and link list
after that, you will know how to work with pointer

oh, better pick a book of C++ style
with or without destructor could be a big difference on resource management


Jan 25, 2011 at 5:03pm
cnoeval wrote:
Instead, you just pass along a pointer to the location in memory where your data structure resides to the function and the function operates on the actual data 'in-place'. This is called pass-by-reference.


At risk of sparking another holy war, if you pass a pointer by value, isn't that strictly speaking pass-by-value?
Last edited on Jan 25, 2011 at 5:05pm
Jan 25, 2011 at 6:06pm
But a pointer by itself isn't really interesting or meaningful. When you pass a pointer or reference to an object, you're passing the object by reference.
Jan 25, 2011 at 6:21pm
this may sound really stupid but the thing that confused me about pointers was every time i saw a star i thought it was another pointer. since it is also used as a dereference operator understanding that kinda helped since its doing something entirely different. not trying to confuse you more just want to help.
Jan 26, 2011 at 1:28am
What confused me about pointers were the * and & signs.I didn't get how to use them.Them i looked at a few source codes,other people wrote,read a tutorial once more, then wrote my own short program that uses pointers and it solved my problem.

Jan 26, 2011 at 2:25am
I just don't know what to do with pointers. I haven't seen any use for them in my programs , that's why I wanted to make a program that uses them were I could understand the point of pointers(bad pun). If that makes any sense. I looked up linked lists and it just went in one ear and came out the other.
Jan 26, 2011 at 3:30am
 
I looked up linked lists and it just went in one ear and came out the other.

start from stack or queue, they are easier than link list
Topic archived. No new replies allowed.