23)

Pre-cursor to pointers

Page 23 Pre-cursor to pointers Additional doc- comparch.zip (160 kb)
Pointers are probably the hardest thing to understand in C, it's not specifically that they're complicated, but difficult to explain and visualise. The other thing that is hard to grasp with pointers at first is there usefulness. Bearing all this in mind, i'll have a stab at explaining them, but it probably won't be the best explanation of pointers in the world. This page is probably going to be big though, and contain mostly theory. If it looks like it's getting too big, the explanation will be split between this and the next pages.

Although there is no code for this page, there will probably be "snippets" of programs throughout, by now it should be safe to start just putting code snippets in, without all the "#include" and "void main(void)" etc...

I've included the download at the top for additional reading, as it may be useful. I just dug out some notes from my old days as a lecturer ( I bet you're thinking, how can someone who explains things this badly be a lecturer - don't ask me, I just bluffed my way through it ! ). They explain computer architecture at a low level, right down to the nuts and bolts to around about HND / 1st year degree level, and not too difficult to understand ( well as far as my explaining goes anyway ! ). You will need Microsoft Word to open the doc file ( I don't know if the images will display right though, word has a funny habit of altering things around for me ).

 

Memory (RAM) storage + number systems
To understand pointers, unfortunately it is necessary to delve deep into how the computer uses memory, and even touch a little on binary. The strange thing though is that without realising it, pointers have been used in most of the programs so far, without even realising it.

On an earlier page it was proven that computers only store numbers, and not characters or words etc, instead they use a code (ASCII) for them. The truth of it all though is that computers don't even store numbers either ! Storage ( whether it be memory or disk ) is just a massive collection of "bi-stable" elements, in other words, things that can have two states like a switch ( on or off ). It's not the job of a programmer to understand how these elements actually work ( silcon, magnetism etc ), that's the job of the hardware people, all a programmer needs to understand is that they exist and can have one of two states, normally depicted by a 1 and 0. The best visualisation though it to think of light switches ( not dimmer swithes though ! ).

Now the maths bit comes in :( - how to make a number like 37 out of light switches ? The answer is by using a number system called binary. By combining these switches and setting each one you can make numbers. We use a system called denery ( base 10 ), this and the next part may seem obvious, but it's important to understand.

In base 10 there are columns ( units, tens, hundreds, thousands etc.. ). So the number 37 is made up of 3 tens and 7 units, as shown below.

1000's 100's 10's 1's
0 0 3 7

Even though this may seem simple, there are a few things to note. Firstly, that it doesn't matter how many column's to the left there are if they contain 0's. For example another column 10,000's could be added which contains 0 and it doesn't affect the number, it's still 37. If 0's are added to the right though, the number changes, if one 0 was added it would become 370 not 37, as to fit that 0 in would require all the other columns to be shifted one place to the left. The second thing to note is how the column values ( headings ) are made up. They start at 1's, then each column to the left is multiplied by 10, eg 1's, 10's, 100's etc. The third and final point to note is that you can only put 10 possible values in each column ( the digits 0-9 ).

By using denery like this you can make any positive whole number up you like. Notice how 10 seems to be mentioned a lot, that's because this is base 10. Computers though don't use it :( they use base 2 ( binary ).

The binary system is exactly the same as base 10, apart from it is base 2. The table below shows how the number 37 would be stored in binary.

128 64 32 16 8 4 2 1
0 0 1 0 0 1 0 1

So, to a computer the number 37 is really a collection of switches set to 100101. The differences between binary and decimal can be seen above. Firstly, this time the column headings don't multiply by 10 each time going to the left, they multiply by 2, starting at 1 still ( 1, 2, 4, 8, 16, 32 etc .. ). Also there are only two digits that can be used to define a number, zero's and one's. Hopefully it can be seen why this is 37 in binary ( 32 x 1 ) + ( 4 x 1 ) + ( 1 x 1 ), just the same as in the denery above ( 3 x 10 ) + ( 7 x 1 ).

Just like denery, you can make any whole positive number using the binary system above, although it's a bit hard for us humans to do, computers do it really well. It's not often though that us humans need to convert decimal to binary and visa versa though. There are some tricks though contained in the attached doc file that can be used if ever you need to do it.

The binary number shown above ( 00100101 ) consists of 8 binary digits ( bits ) and collectively they are referred to as a byte. Therefore, there are 8 bits in a byte. Bytes are important, because computer memory can be visualised as a long series of bytes, more on this later though. Incidentally, a collection of 4 bits is called a nibble ( or nybble depending on which book you read ). You're probably thinking now that a collection of 16 bits is called a munch ! as it happens, it isn't, it's called a word.

When talking of mass storage, the are 1024 bytes in kilobyte and 1024 kilobytes in a megabyte ( guess how many megabytes in a gigabyte :) )

The number 1024 may seem strange, but in binary, it isn't. If you following the binary columns above further to the left ( 256, 512, 1024 ) . You hit 1024 ! which to binary is a nice round sort of number ( like a 1000 is to denery ).

While this is all very interesting, it is hard to see why as a programmer, you may need this sort of information. It's time to reach for the digitherm in FS2 :) ( the current bain of my life ). Find the TACKLE/SHOT folder in FS2, and open the digitherm.tkl file in notepad. On the last line, it reads Device = 31. The number 31 represented in 8 bit binary is

128 64 32 16 8 4 2 1
0 0 0 1 1 1 1 1

If the value in the 16's column is set to a 0 instead of a 1, it will give the decimal value 15 ( 8 + 4 + 2 + 1 ). Change the device line to Device = 15, save it, and run FS2 with the digitherm attached. You will see a difference ! it's turns into an "anatherm" :) ( analog ). So in short the bit position in the 16's column controls whether it is a digital or analog device. The values in the 8, 4, 2 and 1 column also are switches to turn the PH, light, O2 and temp on and off. The values in the 32, 64, and 128 columns are reserved for later options.

Well it was an example to show that an understanding of binary can be useful :)

Besides the use with digitherms, there are far more important places where this understanding can be used, in storage. The next section below, returns to variables and data types again to explain some things that were deliberately missed earlier on page5.htm

 

Storage of variables in memory
The box below shows an amended table for 'c' data types, with a column added to show how much memory storage in bytes each data type requires ( in red )
Type Min Max Storage Desc
char -128 127 1 byte Whole numbers
unsigned char 0 255 1 byte Whole positive numbers
short -32,768 32,767 2 bytes Whole numbers
unsigned short 0 65,535 2 bytes Whole positive numbers
int see note 1 2 or 4 bytes Whole numbers
unsigned int see note 1 2 or 4 bytes Whole positive numbers
long –2,147,483,648 2,147,483,647 4 bytes Whole numbers
unsigned long 0 4,294,967,295 4 bytes Whole postive numbers

Note 1 : The range of integer values depends on the compiler and the operating system a program is running on. If the operating system is a 16 bit O/S like MS DOS then the range of the int and unsigned int data types is the same as the short and unsigned short data types above. If it is a 32 bit operating system like Windows 95 onwards, the range of the int and unsigned int data types is the same as the long and unsigned long data types above.

If you concentrate just on the "unsigned" data types ( char, short and long ), then you may be able to see why the min and max range is set to the values they are. If you've read and understood the attached word doc, then you will probably see why the other data types have the min and max range values too ( two's complement binary ).

Using the simplest example ( unsigned char ), it is stated in the table that it requires 1 byte of storage, and that the minimum and maximum range is between 0 and 255 inclusive. The reason for the stange range ( 0 - 255 ) is because that's about all that can fit in a byte ( see below )

The value 0 stored in a byte (0+0+0+0+0+0+0+0)

128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0

The value 255 stored in a byte (128+64+32+16+8+4+2+1)

128 64 32 16 8 4 2 1
1 1 1 1 1 1 1 1

You'll just have to take it on word of mouth ( or prove it yourself ) that all other decimal values between 0 and 255 can be stored in a byte. Values higher than 255 cannot, however be stored in a single byte, two or more bytes will be needed, hence the unsigned short data type. All storage units in programming are combinations of bytes ( 8 bits ), in other words there is no 12 bit data types for example. If 12 bits are needed then a 16 bit data type would have to be used.

There is an easy way though in a 'c' program to find how much storage each data type uses. It was shown on the previous page with the sizeof function. The code snippet below can be used to prove the table above is correct :

printf("It needs %d byte(s) of storage to store an int\n",sizeof(int));

 

Summary
Well, it does need at least two pages to explain pointers. This page didn't go into pointers at all, but it contains useful information which will help to explain pointers a bit better. Regardless of the usefulness with explaining pointers, it is also invaluable information when you start to add advanced program logic into programs.

The attached document, explains binary and number systems in a lot more detail than this page as well as a number of other useful things too. It's not important to understand it all though.

The next page will be more practical and go on to explain and use pointers in a program, as well as explain memory storage further.

 

Tasks

There are no tasks here, but plenty to go at throughout the attached word document.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001