1. 2.6 Arrays of Pointers Continued : Part One Arrays of Pointers Continued... Part One

Important Announcement

Beyond this point, video lessons are not available. Further, lessons beyond this point have not been fully edited. You are welcome to proceed, but I recommend that you check back later when these lessons have been finished. At this point you might wish to consider enrolling in online classes. If you are interested in doing this, please add "carlherold" to Skype (Skype is free available at www.skype.com) to discuss this option further.

If you would like to keep up to date regarding when new lessons are completed, please subscribe to Our Reddit Page.



Lesson Transcript.


This is a complex topic, so I am splitting it into two lessons to make it easier to grasp.

In this lesson we are going to continue the project we started earlier, when I showed you how to use 10 bytes of memory simultaneously for various purposes.

Recall that our ten bytes look like this:


    B0 B1 B2 B3 B4 B5 B6 B7 B8 B9
 



We allocate our ten bytes like this:


    char *main_pointer = malloc(10);
 



That gives us ten bytes to work with, and our pointer main_pointer is now pointing to the first byte (Byte #0, or B0) of our ten bytes of working space.

Now, we need to create two integer pointers. We will point one of them to B0 and the other to B6.

Remember, this lesson is just a demonstration and is only for illustrative purposes. I just want you to see that this can be done, and how it can be done.

Now, pointing an integer pointer to the start of our ten byte array is easy. We already have a pointer that points there called main_pointer. Since it already points to the right address, the only thing we need to do is create an int pointer using the main_pointer with a cast.


    int *int_pointer1 = (int *) main_pointer;
 



Based on the last lesson, you should understand what this does. Now, we have one integer pointer called int_pointer1 which is pointing to B0 in our ten byte memory space.

Now let's create a second pointer, which will point at B6:


    int *int_pointer2 = (int *) (main_pointer + 6);
 



Remember that main_pointer + 6 is just a different memory address. We are doing the same thing as before, only now int_pointer2 will point to B6 instead of B0 in our ten bytes.

So far so good.

Now there is just one problem. These pointer names are not good. It is poor practice to name two related items as int_pointer1 and int_pointer2. Why not just use an array of int pointers? In this lesson I am going to show you how.

First of all, one way to create an array is simply to allocate the memory that the array will require. In this case, we will allocate array space for two integer pointers.

How much space do we need? Well, let's consider how two integer pointers will look like in memory:


    Horizontal View:
         [Integer Pointer #1][Integer Pointer #2]...

    Vertical View:
         B0 : [Integer Pointer #1]
         B4 : [Integer Pointer #2]
 



If we assume that any pointer is 4 bytes in size, then we need 8 bytes of space to store two such pointers. Because we need to allocate 8 bytes, we need the malloc() command. It will work like this:


    malloc(8);
 



Except, not quite. If you recall from earlier lessons, you should always use sizeof() for any data type so that you are absolutely sure that your program will work for as many computers and compilers as possible. In this case, it happens to be that 8 bytes is correct. That is not guaranteed to be the case all the time. Therefore, we write this instead:


    malloc(2 * sizeof( int* ) )
 



This will give us the same result. This just means we are allocating 8 bytes of storage space for our array of two pointers. Any time we use malloc() we need a pointer to point at the space we allocated. Well, what kind of pointer do we need?

To answer that, we need to answer this:

What will our pointer point to ? It will point to one star ints. Why? Because we are creating an array that will consist of (int *) pointers.

What do you call a pointer which points to one star ints ? A two star int. Similarly, what do you call a pointer that points to four star ints ? A five star int. Any "higher level" pointer is simply one that points to one level lower.

Here is how we create our two_star_int which will point at our array of int * (one star int) pointers.


    int **two_star_pointer = malloc(2 * sizeof( int * ) );
 



If you are confused, let me explain a bit more. We are creating an array of int * pointers. In other words, we are creating an array of one star int pointers. Whenever you create any array, you need a pointer that can "point to" elements of that array. In this case, each element of the array will be a one star int. We need something that can point to a one star int. That means, we need a two star int.

If all you have gotten out of this lesson is that we need a two star int to point at our array of one star ints, then you are fine.

Now consider the following:

    1. two_star_int is the actual memory address of Byte #0. This will be the start of our eight byte working space.
    2. *two_star_int is "what is at" that memory address. What is at that memory address? A one star int.

... Continued on the next lesson ...

When you have finished this lesson, proceed to:

  1. 2.7 Arrays of Pointers Continued : Part Two Arrays of Pointers Continued... Part Two