- 8.1 Introducing arrays and pointers part one In this first lesson on the subject of arrays and pointers, you will learn what a pointer is as well as unique ways to understand memory.
Lesson Transcript.
This function assumes that all text strings are encoded as ASCII. Another assumption being made is that the "unsigned short int" data type is two bytes in size. This is not always the case, so you should be aware of that when reading this lesson.
In an earlier lesson we learned that we can use the printf() function to display text.
Lets briefly look at this text: "abc123"
Recall that it is encoded in memory like this:
0110 0001 : 0110 0010 : 0110 0011 : 0011 0001 : 0011 0010 : 0011 0011 : 0000 0000
"a" : "b" : "c" : "1" : "2" : "3" :
We store text in memory by creating a "train" of ASCII characters, then we end that train with a "null" character.
This entire "train" is stored in memory exactly as I showed above. Every character immediately follows the character before it. In computing, the word used for this is "string".
A "string" is one of the simplest forms of something called an "array". An array is a collection of data elements where each data element has the same data type. For example, in a string of text, you have a collection of data elements (characters) where each data element in this case has the data type char.
Arrays are incredibly useful in programming, and we will get into them more later on. Arrays are also often a source of misunderstanding for beginners, so I want to cover a few important points.
Remember from an earlier lesson that you never have to worry about the actual address in memory where a variable is stored, because this is done for you by the programming language. Also remember that you can give plain English names to variables.
Lets consider this code:
unsigned short int total = 5;
What is "total" ? It is both a way to refer to the address in memory where the value 5 is stored, and it is a way to refer to the value 5 itself.
Every variable has some address in memory. This address in memory is not the value of the variable. Theoretically, the variable "total" might exist at any of billions of possible addresses in memory - you have no idea which one. All you know is that indeed at some location in memory you will find this sequence:
some address in memory : 0000 0000 0000 0101 <--- This is our two-byte "unsigned short int total"
Now, for the sake of this lesson, lets give your computer a massive downgrade in RAM. Instead of you having gigabytes of RAM, you now only have 16 BYTES of ram. Lets examine how this would look.
On the left, I am going to put the address in RAM. On the right, I am going to put its contents - we are going to start with a blank slate of all zeroes to make this lesson easier.
Each address will be 4 bits in size (which gives us sixteen possible addresses in memory). At each address, there will be one BYTE of actual data stored - eight bits.
0000 : 0000 0000
0001 : 0000 0000
0010 : 0000 0000
0011 : 0000 0000
0100 : 0000 0000
0101 : 0000 0000
0110 : 0000 0000
0111 : 0000 0000
1000 : 0000 0000
1001 : 0000 0000
1010 : 0000 0000
1011 : 0000 0000
1100 : 0000 0000
1101 : 0000 0000
1110 : 0000 0000
1111 : 0000 0000
Now, lets imagine also for the sake of this lesson, that "unsigned short int" is only one byte in size, instead of two. Lets re-consider the following code:
unsigned short int total = 5;
Now, your programming language is going to choose somewhere in RAM to put this. This is as far as you are concerned entirely arbitrary. You have no idea where in RAM this value 5 is going to be placed.
Lets imagine that the variable "total" gets put in the memory address "eight" in our sixteen bytes of ram. Here is the new ram table with this modification:
...
0101 : 0000 0000
0110 : 0000 0000
0111 : 0000 0000
1000 : 0000 0101 <---- here is where we stored the variable "total"
1001 : 0000 0000
1010 : 0000 0000
1011 : 0000 0000
...
We can see therefore that the variable "total" actually refers to two different values. Five, and Eight. Eight refers to the location in ram where "total" is stored. Five refers to the numeric value stored at that location.
Lets go back to this statement:
What is "total"? It is both a way to refer to the address in memory where the value 5 is stored, and it is a way to refer to the value 5 itself.
This should make more sense to you now. We will talk more about this in the next lesson.
When you have finished this lesson, proceed to:
- 8.2 Introducing arrays and pointers part two Here we explore arrays and pointers in more detail, and you will begin to see how to use them in programs.