views:

229

answers:

10

Hi, right now I'm trying to teach someone about C programming. Specifically right now we're on pointers in C. However I feel this is a difficult concept to explain, as the person I'm teaching doesn't have much programming experience. I was wondering if anybody knew of a good tutorial on C pointers for a true beginner programmer?

I've tried searching on google however the results seem to assume the reader knows a lot.

+1  A: 

Nice graphical illustration....

http://cslibrary.stanford.edu/106/

Simon Lee
or this one... http://computer.howstuffworks.com/c22.htm
Simon Lee
both of these sites seem to be good resources
Albinoswordfish
A: 

Check these -

http://www.taranets.net/cgi/ts/1.37/ts.ws.pl?w=329;b=277

There is a book called "Pointers in C" by Yashwant Kanitkar which is really a good book to read and understand. Not sure if online version is available.

Sachin Shanbhag
Awful book... Kanetkar is a _horrible_ author.
Chinmay Kanchi
+3  A: 

I wouldn't be teaching a beginner pointer arithmetic. It's probably better to start with a language that doesn't require that knowledge. Teaching someone C from the get go is like throwing them off a building and demanding they fly. I recommend starting with Scheme or maybe python. Once they get a grasp of data structures, and scope, and start wondering what's happening behind the scenes then I'd suggest C and pointers.

OmnipotentEntity
+1 For the point that C is a horrible first language to learn on.
T.E.D.
Disagree (not upvoting). I'm not saying that people should avoid Python or Scheme as the first langauge, but I think a lot of people feel that the closeness to memory offered by C is a nice way to get familiar with the fundamentals of programming. I certainly felt this way, but YMMV.
gspr
@gspr, I agree with this
Albinoswordfish
Agree with **OmnipotentEntity**, disagree with **gspr**. Knowing the details of memory management *is not important* for getting things done with programming in the modern age - because all the tools you *should* be using for trendy projects do it for you. It is *very* important to software engineering and computer science though. It depends what you want to achieve and what the beginner's real interest is. It's a lot easier to keep a novice's attention and enthusiasm with high-level and encourage them to move more low-level if they're interested, than vice versa.
Tom W
I completely disagree... the idea that you shouldn't need to even look at memory management as most modern languages deal with it for you is a VERY dangerous idea. Having come from Delphi, .Net and now Objective-c I know that having a knowledge of memory use regardless of whether the language takes care of it is a very good thing. I have seen LOTS of sloppy code from other developers because "I don't need to worry about that.", and when these developers move to a more memory intensive language it is people like me that end up fixing their bad code! Knowledge is a good thing.
Simon Lee
There's a large difference between "I don't need to learn pointer arithmetic" and "I don't need to worry about managing how much memory my program is using." The point of teaching someone a higher level language at first is so they get a grasp of algorithmic thinking (which is a difficult concept for many people to learn) before being thrown to the world of esoteric bugs that occur when using C as a beginning programmer. I don't know about you, but when I first started programming debugging my code took up 90% of my development time, mostly because I couldn't figure out what I was doing wrong.
OmnipotentEntity
+1 @gspr, not only is it a good way to get familiar with the fundamentals of programming, it's also a good way to get familiar with how the *computer actually works*.
Carl Norum
A: 

From my teaching experience, if you start with pointers without mixing them with dynamic memory allocation, they are pretty easy to grasp.

As already mentioned, pictures help a lot.

Once basic pointers are understood, you can move to arrays (still on stack) and pointers and they are related. Introduce pointer arithmetic (specifically the definition of the [] operator).

Once this is understood (very good test is let them implement palindrome testing using pointer arith), you can safely move to dynamic memory. Make sure you use valgrind heavily (not sure what is available on Windows).

Let_Me_Be
+3  A: 

You might want to take a look this book: The C Programming Language, by Brian Kernighan and Dennis Ritchie

gcc
A: 

Here is an excellent, short tutorial. I kid; I wrote that. But it may have some value.

Ziffusion
A: 

If the student understands memory (bytes and words) and how variables reside within memory, it should be no problem for them to understand pointers (memory addresses).

Addendum

To clarify: the student needs to understand how variables are stored in memory in order to understand that pointers are variables whose values are memory addresses.

Loadmaster
Your answer to *"how an I help someone who has trouble understanding pointers to understand pointers"* is *"if he understands memory, he should not need help understanding pointers"?* Wow, that's... not very helpful. Why not just ignore the question instead? -1.
gspr
I guess you missed my point, which was that he has to understand how variables reside within memory, specifically that they are located at memory *addresses*, before he can understand what a *pointer* is.
Loadmaster
In light of the addendum I removed my downvote :-)
gspr
A: 

Think of pointer as an generalized idea of array index. And as index may point to array element, pointer may point to any variable in memory (imagine memory as great array!).

Accessing to array through index and variable (and array ofcourse) through pointer is much similar: index: array[i] pointer: *p

Compare two programs, one uses indexes, and another uses pointers

long x[20];                   long x[20];
int i;                        long *p;
x[0] = 0;                     x[0] = 0;
x[1] = 1;                     x[1] = 1;
for(i = 2; i<20; i++) {       for(p = x+2; p < x+20; p++) {
    x[i] = m[i-1] + m[i-2];       *p = *(p-1) + *(p-2);
}                             }

That's the analogy. Hope this helps.

Vovanium
+1  A: 

This is not really an answer, since I don't have a tutorial to point you to. I just want to follow up on the comment that said "draw lots of pictures" by saying: Yes! And find a good analogy you're comfortable with. Analogies aren't always cheesy and don't always begin with "it's kinda like a car..."

Here's one that helped me a lot: Try thinking about pointers as strings -- no, not text strings, but the real-world-piece-of-rope kind of string that you use to tie things with! The rôle of your data is played by any real-world object in a really peasoup-foggy place. Maybe you can't even see the data from where you're standing.

Thus:

  • By having a pointer, you're holding one end of a long piece of string. Follow it, and you'll find data at the other end.
  • Copy a pointer: Two ropes leading to the same data.
  • Delete a pointer: Drop a piece of rope. Now maybe you can't find your data in the fog anymore, so watch it.
  • Copy data: Follow a rope and, ehm, clone (real world analogy-fail), the thingy at the end. Be sure to tie a new piece of string to the clone or you'll lose it in the fog.
  • Delete data: OMG, now some piece of rope might lead to nothing (or to a cliff filled with lions and dragons!).

This silly analogy helped me a lot. I know it doesn't cover pointer arithmetic, but I think that's easier to grasp if you feel at home with pointers in the first place.

gspr
A: 

pointer are "address" like postal address. When you want to store something in the pc, it's like on earth, you must create a "storage house". This is called memory allocation. Once the storage house is built, you can store and get back thing into it. To do it you use the address of the storage house, that's the pointer. To access the content of the pointer, you write *pointer, etc... Hope you understand the idea :)

Thomas