24)

Simple pointers

Program 22 Simple pointers Source code - prog22.c
Now that the way computers store numbers is known, it is possible to explain pointers in 'C' to a level where it should be clealy seen what they really are. While it may be easy to understand what they are, it may not, at first be apparent how and why you use them. This hopefully will be seen in subsequent pages following this one.

The example program is pretty useless apart from to show how pointers work, and how a program can get details of the way memory is stored and allocated. Even though it looks a simple program, the description should be read through slowly. Also, like a lot of the other programs, you can probably understand it a 100 times better than my description, by running the program and seeing what is going on.

 

Program code- new parts shown in red

#include <stdio.h>

void main(void)
{
unsigned char age=21;
unsigned char *ageptr;

ageptr=
&age;
printf("The variable age takes %d byte(s) of storage\n\n",sizeof(unsigned char));
printf("It is stored starting at memory address
%p (hex)\n\n",ageptr);
printf("The contents of memory address %p are %d\n\n",ageptr,
*ageptr);
printf("The variable age also happens to contain the value %d",age);
}

 

Description of the program code
Imagine memory as being just a large collection of pigeon holes that can hold a byte, which run in order, labeled from 0 to x ( with x being however much memory a computer has ). Eg, if a computer had 4Mb of memory installed it would have 4,194,304 seperate bytes of storage ( 4 x 1024 x 1024 ).

When the line unsigned char age=21; is encountered, it effectively says, "find me 1 byte of free memory, put the value 21 in there, and i'll refer to it as age from now on". This is why variables are so useful, to the computer, this isn't known as "age" but really a memory address somewhere, but to a programmer, referring to age instead of a memory address is far more meaningful.

The next line down unsigned char *ageptr; is declaring a variable called ageptr like before, but this time an asterix (*) has been inserted before the name of the variable. This tells the compiler that this is a variable that will hold a pointer to an address somewhere in memory. A pointer can be thought of as a signpost, which points to somewhere or something. Here it is being told that it will be pointing to a single byte in memory ( unsigned char ).

The line ageptr=&age; is assigning a value to the ageptr variable. The ampersand (&) can be read as "address of". So the line is really saying "store the memory address of where the age variable is located in the ageptr variable".

The next line, printf("The variable age takes %d byte(s) of storage\n\n",sizeof(unsigned char)); just displays on the screen proof that an unsigned char does take up a single byte of memory. If it makes the program more readable, it is also okay to have the line reading printf("The variable age takes %d byte(s) of storage\n\n",sizeof(age));

The following line, printf("It is stored at memory address %p (hex)\n\n",ageptr); displays the actual memory address ( in hexadecimal ) where the age variable is located, or if you prefer, it displays the contents of the ageptr variable, which holds a pointer to the memory address where age is stored. The %p format specifier is used because this time a pointer value is being displayed.

The next line, printf("The contents of memory address %p are %d\n\n",ageptr,*ageptr); shows how to not only retrieve a memory address, but also the contents of that memory address. By using the asterix (*) in front of a pointer variable like this, it is effectively saying "the contents of". This line proves that, when the program is run, there is a pointer to the age variable, because it displays the value 21.

The final line has been used before, and requires no explanation.

Remember, this program isn't a useful example, but it's a simple one to show the working of pointers. Why you would ever want to write a program that displays memory addresses of where variables are stored, I don't know, but it's there :)

You may be wondering what happens to the memory addresses after the program finishes ? or even thinking that, if you run this program millions of times, you will eventually run out of memory. This isn't true, because as soon as a program terminates, it free's up the memory allocated by variables, so that there addresses can be used again. This happens automatically and it is the operating system's job of doing this and managing memory.

 

Summary
Well that's pointers :) there is, of course a bit more to them yet, but essentially the program above shows them for what they really are. They are really useful, honestly and used all over the place. This may not seem obvious though yet, but the next page which explains "dynamic memory" will prove this and how without them programs would be very restricted and static.

Also, it should be pointed out that even though the explanation of how memory is allocated and stored is true, there are a few other complications, like "virtual memory" in windows. As a programmer though you don't need to worry about this as it is all handled, like a lot of things by the operating system, silently in the background.

 

Tasks

No tasks here.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001