Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'll hold a little presentation about C-Pointers in a small group of people with beginners programming skills, with no CS background. Just people who would like to learn something about coding and prefer a practical hands-on approach. In fact, they are using the C-Language to do so.

We are about to introduce pointers and I am very careful about the scope for the first introduction, since pointers can be quite overwhelming and confusing for the uninitiated.

So I am wondering, how would you approach this topic to make it fun and interesting? Do you have great examples that really help people to understand the concept of pointers?

Hint: The audience doesn't know about strings and arrays yet.

Scope-wise, here is what I've got so far:

  • Example: A problem that shows the limitation C without pointers and why we need something more elaborate.

    • Call by reference vs Call by value example?
  • Very short introduction what a pointer is and how it is denoted in C. (* and & operators )

  • The difficult part: How does a pointer work? Or: How to explain a technical problem to non-technical people?

    • Looking for a real work example for a referencing system. Is a library a good example?
    • What is memory and how does it interact with our code and variables?
    • How does a variable look like in memory? (memory allocation)
    • How is this variable accessed? (address referencing)
    • How can I use a pointer to access memory?
  • In dire hope, people can follow this train of thought:
    • Live example and practice

Things I would like to omit for a part2:

  • Null-Pointer
  • Pointer on Pointers
  • Pointer arithmetic
  • Adresses vs. Pointers
  • Structs
  • Arrays
share|improve this question

closed as too broad by gnat, Neil, amon, Bart van Ingen Schenau, MichaelT Feb 19 at 13:31

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
teaching C to beginners is very ambitious, I'd have preferred a language with less gotchas –  ratchet freak Feb 19 at 10:26
1  
It's not the first lesson. They already have a basic understanding of data structures, control structures and the like. Those people are keen to learn about the C-Language, while having not much knowledge about computer systems. I agree with you it is hard for students and me. Nevertheless I need to find a way :-) I dont' need to squeeze as much information as I can into a single lesson. I can make multiple sessions, to make sure everybody will be able to catch up with pointers –  Dr.Elch Feb 19 at 10:31
5  
The things you're omitting would make for great introductions. Arrays are wonderful, but they have to be statically allocated. Contriving a simple example where you don't statically know the input size, then explaining malloc would probably be a fine idea. What is this thing that malloc returned? Why can I use it just like an array? Call by reference is probably too confusing an example before your students know about pointers (unless you're using C++, which has a nice alias syntax). One important thing is to be honest about C's memory model, don't perpetuate the “pointers are hard” FUD. –  amon Feb 19 at 10:45
1  
Long ago.. I had a tutor that explained pointers to us with white boxes on the floor. We all stood up and each box had a card in it denoting the type located at that block of memory (int for example spanned four boxes.. a char only one). He then had a student point at a box and used his slides to show what incrementing did by having the student point at another box. I think he executed it really well and it got the basic understanding across. –  Simon Whitehead Feb 19 at 12:42
1  
@BobCross the fundamental problem with this question (that even your suggested edit didn't fix) is that it is a poll of opinions (thus the too broad) of "how would you approach this topic to make it fun and interesting? Do you have great examples that really help people to understand the concept of pointers?" - this would need to be fundamentally reasked in a different way, preferably posing a problem that the OP faces rather than the general fishing for ideas. –  MichaelT Feb 19 at 19:08

3 Answers 3

tl;dr: It's all about the pictures.

C Programming for non-majors is the first class I ever taught. I had good luck with pointers by introducing them as a concept fairly early and letting them sink in as not-scary. Then I snuck in progressively more complex examples over the semester without drawing too much attention to the pointer syntax (other than as a reminder of how it works). Instead, we focused on "see, we're solving more useful problems - you're writing real code now! (oh, and you're using pointers quite a lot but don't mind them)."

Addressing your specific points:

The difficult part: How does a pointer work? Or: How to explain a technical problem to non-technical people?

This is where I started drawing many pictures.

One of the first was a sketch of a spinner from a board game (i.e., that needed random numbers but didn't want to use dice): that's usually something that everyone in the room could relate to. The needle (it's not really a needle but I didn't want to say "pointer" too soon) in the center is a physical thing. It can only point to one box at a time. The boxes are all separate from each other. Each box held a number. We could spin it and ask "what number is the needle pointing to?"

At this point, the whole room would be nodding when I would say that the C language contained both concepts: memory for holding values (i.e., the boxes) and pointers for indicating a particular memory "box."

Very short introduction what a pointer is and how it is denoted in C. (* and & operators )

At this point, since we were all comfortable with the idea of arrows AKA needles AKA pointers and the memory AKA boxes that they could point at, we could write some simple one liners and draw the pictures that are their equivalents.

NOTE: This is where class participation became critical quickly: "but what if I want to ...?" meant that the room was engaged. Pictures, pictures, code and more pictures!

We would generally start with simple examples:

int x = 1; // A box with a 1 in it
int y = 2; // Another box way over on the other side of the board with 2 
int z = 3; // Yet another box with a 3

int *p = &x; // See everyone?  All this does is make the arrow that points at box x
int *q = &y; // A pointer that points at y

printf("%d", *p); // "What's in the box that I'm pointing at?" => prints a 1
p = q; // Ooo, now both arrows point at box y
printf("%d", *p); // "What's in the box that I'm pointing at?" => prints a 2

*p = 5; // What does this do, everyone?  If I printed *q, what would I see?

... and so forth. The critical point was to drill the syntax home. Practical examples came later after everyone was clear on the mechanics.

I think of pointer syntax as being at least as important for introductory C programmers as the difference between = and == (or worse, & and &&). If you can't even read the syntax, more interesting problems will be impossible to solve.

share|improve this answer

When I was taught about such things, the best approach to me was to draw it out.

Draw out a line of boxes. These are the memory "cells". At this stage we can gloss over the differences between static, stack and heap memory. You don't even need to go into details of how many bytes a variable needs.

Each box has an address - a simple number on all modern computers. So write a series of numbers next to the boxes.

If I declare a variable, int foo = 5;, then the compiler allocates a box, puts a label "foo" on it, and puts the value 5 into the box. Let's say this box has address 1000.

If I declare a pointer, int *fooPtr = &foo;, then the compiler allocates another box, puts a label "fooPtr" on it, and puts the address of foo into it - i.e. 1000. The unary "&" operator is "get the address of".

The unary "*" operator is "get the thing pointed to by". So if I write printf("%d", *fooPtr);, then it will look in fooPtr, see the value 1000, go to the box with address 1000, and fetch the value 5 from it.

That's pointers in a nutshell.

Pointers-to-pointers, arrays and pointer arithmetic can later be explained using the same line of boxes analogy.

share|improve this answer

Whatever the content you wish to cover, if it is a beginner audience you are going to present to, it is probably best to start off with a few slides explaining a schematic of the guts of the computer architecture you are going to use (have a box representing the ALU, slots for memory, etc...).

Everything you have listed there will make more sense to the audience if they have a high level schematic of the machine in their minds. You can find one in a freshman's book on computer organization and architecture.

Teaching pointers is not the hard part. Pointers are easy to understand if you have a high level overview to guide you. Pointers are difficult to grasp if you have no idea what an abstracted machine looks like.

It is best to cover a few slides on computer architecture and the compilation process before you dive into pointers.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.