1/03/2012

NCC Tutorial: Pointers 1

This is my first language tutorial ever, and I hope it will be helpful. I chose to write about pointers first, because that's the way everything works. The pointers are The Thing in C language. If you get comfortable using them, you will be able to really love C. Otherwise, you'll probably hate it. So, let's start with the first tutorial here.

In order to understand pointers, you need to know how computer memory works the way we see it as programmers. One way is to look at the memory like it's a checkerboard. Every checkerboard field has its own address (you know, columns A-H, rows 1-8, addresses A1-H8). Similarly, every "field" in memory has its own unique address. The checkerboard way is actually the way RAM is organised physically.

The other way we look at the memory is linear. Every "field" in memory has its linear address. The size of an addressable field is one byte (1B), so every unique byte in your RAM has its own linear address. Linear addresses are nothing but unsigned integer numbers, from 1 to whatever the size of your memory field is.

When you need a place to store a variable, you get a field (or particular number of consecutive fields) from the memory, and you use it especially for the purpose of storing the value of that variable. When you write
int x = 10;
the computer finds a free place in RAM, assigns it to the variable x and stores value 10 in it. The next time you need the value of the variable x, you just use x as a reference to the location in memory where 10 is stored.

As any other, the memory location where 10 is stored has its address. If it happens that the value written in that address got changed, that would mean that the value of our variable x is not 10 anymore. If you try to use x after that, the calculations probably wouldn't give the same result as before.

How you can change it without using x?

Our x is a logical name we assigned to a memory location, so that we can easily address it and understand what we wrote in our source code. If we wanted the linear address of that field, we could use reference operator "&", like in &x. This will give us an unsigned integer which represents the linear address of the location where x is stored in memory.

OK, now we know a way to get the location address. What we need is a way to change its value. The solution is dereference operator "*", like in *(&x). Now this is where it can get tricky. The * operator is used to mark the value that is stored at the address after it. So, basically *(&x) is the same as x.

And, finally, let's see what the pointers are. And they are not anything else than the variables in which various addresses are stored. Why pointers? Because when you have a variable which represents an address, than you basically use it just to "point" at that particular memory location.

We define a pointer variable by just adding an asterisk (*) before it. I.e. if we need a pointer to an integer, we can define it like in:
int *p;
p is the variable, and * is used just to tell the computer that's a pointer.

If p has assigned address to point at, then the value of p is that address. You can get the int value on that address using *p. &p would give you the address where p (the pointer) is stored :)

In NCC Tutorial: Pointers 2 there will be some examples on how to use pointers to play with memory and change values of some variables without actually using them. Stay tuned!

No comments:

Post a Comment

If you have anything useful to say (ideas, suggestions, questions, business proposals, or just "Thank you!"), please feel free to comment! Also, you can use my e-mail and/or find me on Twitter!