Last Updated: January 20, 2020
·
3.176K
· ericraio

The Concept of Pointers

When learning C, I found that learning pointers was one of the more trickier things to learn. Coming from a JavaScript/Ruby background the idea of a pointer has always been abstracted away. The concept of a pointer is really simple, what threw me off is the syntax. At first to me it wasn't so clear because the syntax didn't really match the concepts, until I started writing code to understand it. After months of playing with pointers in C it just gets more simple. The book Learn C The Hardway was nice because it is straight forward and gets you writing code using pointers. Once you understand the concepts of a pointer and when you start writing enough code using pointers, it'll click and you will get that oh-familiar "Ah-ha" moment.

I wanted to go through some of the concepts on pointers and then you the reader will have to just start writing code.

Direct/Indirect Referencing

First thing to understand is the difference between direct and indirect referencing and that pointers are indirect referencing.

Given the scenario, say we have a classroom with 50 desks, 10 desks across and 5 rows back, we then assign each desk with a number up to 50. Now all the students come into class and sit down in their desk.

Let's say there is a student named Jim and he happens to have the dry erase pen needed to start class. When I call jim out loud he then stands up and hands me the pen. This would be a direct reference. I am directly calling jim not caring where he sits in the class room and he gives me the pen.

Now Jim happens to be sitting at a desk with the number 36 on it. If it were the first day of class and I did not know Jim's name at all, if I were to say "Desk number 36 I need the pen." The person comes up and gives me the pen. I did not have to know who jim was to get what I needed to start class. This is indirect referencing and that is the concept of pointers with memory.

You can think of the memory as all the desks in the classroom and the students that sit in each desk is like what is being stored in memory. We always know the address of memory just like we know the number assigned to each desk in the classroom.

10 Responses
Add your response

I personally think the best way to explain pointers is like this (note that I've only ever explained this to people that have learnt Python first, so I'll use Python terms despite them lists and arrays being different things in real life):

A pointer is a memory address. What does that mean? Imagine memory as a giant list (i.e. array in non-Python terms) of bytes, of fixed length. Now imagine that a point is a number being used as an index into that list.

over 1 year ago ·

That is a pretty good way of explaining it!

I wrote the article over a year ago and just posted it on coderwall, I feel that pointers can be a little confusing at first but once you understand them, the concept is quite simple.

over 1 year ago ·

As @milesrout said a pointer is a memory address. If you've ever programmed in assembler code, when you're working with pointers, the pointer literally saves the memory address where you want to read/write data.

When you see the translation for example of a C code into assembler where you are passing a parameter as a reference and not as a value, the traduction as assembler converts that parameter into an address that is read inside your function. That would be the exact way a pointer works in low level.

over 1 year ago ·

An excellent way of explaining pointers to those who don't get them. Very well put and an excellent example - Kudos!!

over 1 year ago ·

@shabbirh thanks!! :)

over 1 year ago ·

Simple and clear to those just learning this concept. Well done, Eric.

Also kudos to @milesrout.

over 1 year ago ·

Of course C doesn't actually have pass-by-reference - you pass pointers by value instead.

over 1 year ago ·

I think part of the reason pointers are so hard to learn is because we approach them without understanding what a "variable" really is first - at least this was the case for me. I found this video from Standford Programming Paradigms to be the end-all be-all for me to understand pointers: http://www.youtube.com/watch?v=Ps8jOj7diA0

Although I still get pretty tripped up if I see too many &&&&**&& going on :)

over 1 year ago ·

no code examples in this post?

over 1 year ago ·

@haiqus - I wanted to explain the concept of pointers and what they mean instead of explaining how to use them.

over 1 year ago ·