Recommend viewing at 720p HD full screen. Post any questions or comments in the discussion section below. To view a modified text transcript of this lesson, click here.
Lesson Transcript.
For the purpose of this lesson, assume all text characters are encoded as ASCII.
Lets examine the following code:
char my_char ='a';
Here I have stated that I am creating a new variable called my_char. I have stated to use the data type char for this variable. Now the variable my_char can hold any single-byte ASCII character I desire. In this case, I set the value to the character 'a'. This means that somewhere in memory, I have this:
address of my_char : 0110 0001 <--- "a"
We do not know what the address is, only that there is one. An address in memory is just a number, a sequence of 1s and 0s no more special than any other binary sequence.
Lets suppose that the variable "my_char" sits at position 1000 (eight) in our 16-byte RAM from the previous example:
...
0111 : 0000 0000
1000 : 0110 0001 <--- "a"; Here is my_char at position eight (1000)
1001 : 0000 0000
...
Notice that my_char has a value as well as an address. The value is 'a'. The address is 1000 (eight). Any time you create any variable, it will have a memory address.
Notice that the memory address of an ASCII character 'a' is not an ASCII character - it is a memory address. The data type of your variable is not the data type of the memory address where it is stored. A memory address does not care what kind of data is stored in it. I want to illustrate this using our 16-byte RAM example:
I am going to add some data before and after the 'a' to illustrate this.
0111 : 0000 0101 <--- This is the actual number 5
1000 : 0110 0001 <--- "a"; Here is my_char at position eight (1000)
1001 : 1100 0100 <--- This is actually the start of some unrelated data.
There are three totally different kinds of data above. However, the memory addresses still work the same way regardless of the data stored at that address. There is absolutely no direct relation between the memory address and the data stored at that address.
If I asked you, "Please show me the binary sequence that is stored at position 1001", you have all the information you need to give me those eight bits. The same is true if I asked you, "Please show me the binary sequence that is stored at position 0111", or any other location in our 16-byte RAM example.
Notice that while you can give me the eight bits of data stored at that location, you could not tell me whether it was to be a character, or a number, or something else - as we have learned in previous lessons.
Remember that we have learned that any binary sequence cannot be understood until we give it a "data type". A memory address is a binary sequence, just like any other. Therefore, memory addresses require a "data type" just as ints, or chars, or any other kind of data that might exist.
In computing, there is a term to describe a data type that is designed to hold memory addresses. This data type is called a "pointer". Any time you create a variable of the data type "pointer", you are creating a variable designed to hold a memory address.