Monday, January 16, 2012

Coding Lessons: C and pointers (lesson 14)

In the lessons last year we looked at a the basic variables in C, integers, longs, floats, chars, short integers, arrays or chars and strings (which are just null terminated arrays of chars).

What we didn't think about is what's going on inside the device.

This gets confusing quickly.

so lets try and take things slowly.

Lets declare a variable.

int num;

We've declared a variable we called that variable num, we say that variable will be an integer number.

Behind the scenes what has happened is that the program that we write will take a section of memory that is the size of an integer, (remember from lesson 2 int is 32bit numbers) so a section of memory, (a box) is set-up, that will contain the data than we assign to num.

The box is called num, we can put number data into that box.

num = 23;

Now the box called num has the data 23 in it.

But there is something missing from this whole idea: where is the box?

The box is "somewhere" in memory, it has an address.


So now lets declare a new variable

int *mem_ptr = #

now this is a strange one, first, what's that star all about, and second, how are we putting the string &num into an integer variable?!

Well, firstly, the star specifies that this is a pointer, it's going to point to a memory address.

what comes next is a little weird and interesting.
we're not butting a string into the box we're telling it, that pointer, points to the address of the variable num, that & symbol is the address operator.


so let's look at this in a program.

#include <stdio.h>
int main()
{
int num;
int *mem_ptr = &num;

num = 8;

printf("num = %d\r\nptr = %d\r\n", num, *mem_ptr);

}


When you run this program you get the following output:

D:\coding\lesson14>tcc source.c

D:\coding\lesson14>source.exe
num = 8
ptr = 8


which is pretty obvious.

you're writing, Make a variable called num, that's an integer.
but what you're really saying is go get me 32 bits of memory, I want to store something, I'll refer to it as num...

So then when you use the pointer and say, the pointer has the same value as the address of the variable num (it looks at the same memory location) of course the value is the same, it's looking at the same 32 bits of memory,

as well as looking at the variable by inspecting the same memory location, we can also, write to memory by writing to the memory address that the variable uses by writing to the pointer to that memory address.

#include <stdio.h>
int main()
{
int num;
int *mem_ptr = &num;
num = 8;
printf("num = %d\r\nptr = %d\r\n", num, *mem_ptr);
*mem_ptr = 1;
printf("num = %d\r\nptr = %d\r\n", num, *mem_ptr);
}



D:\coding\lesson14>source.exe
num = 8
ptr = 8
num = 1
ptr = 1



we can also change the memory address that the pointer looks at during the program.

in the following source code we'll set up two variables, one called num and the other called digit, then tell the pointer to look at the contents of the address space given to num, (and we'll compare that to num) then we'll look at the contents of the address space given to digit to show how the pointer can move.

#include <stdio.h>
int main()
{
int num, digit;
int *mem_ptr = &num;
num = 8;
digit = 9;
printf("num = %d\r\nptr = %d\r\n", num, *mem_ptr);
mem_ptr = &digit;
printf("num = %d\r\nptr = %d\r\n", num, *mem_ptr);
}>


D:\coding\lesson14>source.exe
num = 8
ptr = 8
num = 8
ptr = 9



Types of pointer.
We're comfortable that a pointer looks to a memory location, and that can be changed.
so far out pointer

No comments: