tags:

views:

328

answers:

8

I have worked with pointers a lot, but still whenever I work with them, I feel that one might not work the way I expect it to. My mind is not sure as to how they will behave.

What do you suggest? I want to learn pointers better.

+3  A: 

I recommend reading "The C Programming Language" by Kernighan and Ritchie.

Chapter 5 deals with pointers and arrays. This book is very clearly written, contains illustrations to demonstrate the concepts, and it also has exercises you can do to practice. Even better, the book is short.

Kinopiko
No matter how many times you come back to this book, every time you find something new to learn.
Duleb
+4  A: 

Try Binky.

rlbond
+4  A: 

The best way is to write programs. Get your hands on K&R, go through the basics and start writing some programs.
Sample problems could be :
1) singly link list, doubly link list.
2) Basic tree questions.
-- insert a value into a tree.
-- find height, depth, delete a tree etc.

Duleb
A: 

In a nutshell - pointer are just a technique for indirection. For example, instead of passing a number directly (by value) to a routine, you can just pass the address of where the number is stored so that the routine that needs it can just look it up at the address. Now this is interesting because whoever has that address, can change the value stored at that address - and all other holders of that address can see the results of that change.

There can be a lot of messy syntax around pointers when doing pointer arithmetic, or pointer to functions, but the fundamental idea of a pointer is simple if you give yourself some time to contemplate it (and play with a couple of simple examples.)

Good luck,

Paul

Paul
+1  A: 

I agree with Duleb, just write some code.

But, to help you understand, draw out what is going on, as you may have an array of pointers, and you need to walk through, so you have pointers to pointers.

By drawing diagrams you can visualize what is going on, and after a while you may find yourself drawing these images in your head, and it will make more sense.

Unfortunately, until you get used to working with it pointers will probably be a mystery. I don't think you can really understand how to use it by just reading a book.

James Black
+1  A: 

Carefully perform all exercises you can find online -- this one, this one, etc (skip the C++ specific parts, such as "references", if you truly only care about C). Be sure to carefully study or rehearse each prereq material given in the exercise, if any. Once you're done, compare your solution code to the code given as the official solution -- do they both compile and work identically on the examples given? What about somewhat broader variants? Any doubt you get, ask on stack overflow!-).

If you want to check how well you're doing, feel free to ask for more exercises if you haven't found enough online - simple but key ones such as "reverse a singly linked list in-place" or "remove a node from a singly linked list given a pointer to that node" are classics, but really getting them remains a great way to measure yourself on pointer issues. (Single lists are great sources for difficult but important pointer exercises... that may be part of the reason why the still-current C++ standard only offers doubly linked lists, they just don't generate that many "interesting" problems!-).

Alex Martelli
A: 

Remember that at it's heart, all data manipulation, all of your data structures, all of your types, are just moving bits around or tracking them in memory.

Understanding that an array of ints is simply a block of memory the size of 1 int times the number of elements in the array, and realizing that a struct doesn't have some magic delimiters or metabits floating in memory keeping the member variables apart, helps you to remember (realize?) the zen of C programing.

Do you want to learn pointers better? Learn the void pointer. It's the one without any bias, the purest form of a pointer, and think about the values you could realise from the same bits in memory.

It's all smoke and mirrors in the end - unlearn your hard types - and remember the bits in memory. The pointers are less adulterated than the ints or chars, and the void pointer is the purest of them all.

I think it's something every programmer knows, but all too few realize.

Aramis wyler
+1  A: 

Try your hand at some assembly language. This will show you really, really quickly what pointers actually are.

Andrew