Site Navigation [ News | Our Software | Calculators | Programming | Assembly | Downloads | Links | Cool Graphs | Feedback ]
   Site News

   Our Software

   Legal Information



   C Programming

     Introduction to C

     Keyboard Input

     Graphics Intro

     Slider Puzzle 1



     Dynamic Memory

     Slider Puzzle 2


     Bit Manipulation

     Advanced Pointers

       Part I

       Part II

     File I/O

     Graduate Review




   Cool Graphs

   Feedback Form

C Programming Lessons

TIGCC Programming Lessons

The following is part of a series of lessons designed to help teach people to program in C for the TI-89, 92+, and V200 calculators using the TIGCC development environment.

If you wish, you can download the program source code, project files, and binaries here.

Lesson 9: Advanced Pointers

Step 1 - Reintroduction to Pointers

Up to this point, we have used pointers in a variety of fashions. We have seen the unique connection between pointers and arrays, and how we can treat them differently even though they are very similar. We have also touched upon subjects like multidimensional arrays, but have not covered the topic in any detail. This lesson focuses on the more complex use of pointers and their myriad of uses in C. Remember: pointers are the most important concept in C. That's why they are also the hardest.

The first thing we should talk about is multidimensional arrays. We have seen how to create arrays using the [] array bracket specifiers. We have seen how to use arrays using array notation, and how to use them as pointers, because all arrays are pointers. We have even used arrays which are multidimensional, but I glazed over the topic because I wasn't ready to discuss it in detail at that time. Most C programming books and teachers cover multidimensional arrays at the same time as arrays, but this usually leads them to talk about arrays and pointers as though they are separate concepts, which they really aren't. This is why I chose to forego the discussion of multidimensional arrays until now.

We know that to create an array of size x elements, say of type character, we can do this:

char string[40];	// create an array of 40 characters

This tells the compiler to reserve 40 characters in memory to use for this array. We can also create a dynamic array using the memory management functions malloc(), calloc(), and realloc().

char *string = (char *)malloc(40 * sizeof(char));

We have also seen how to create arrays of string literals, like this:

const char *str[] = {"String 1","String 2","String 3","String 4"};

This last example is a kind of multidimensional array. Remember that arrays are pointers, so an array of pointers is an array of arrays, and that is exactly what a multidimensional array is. An array of arrays. We can also create arrays of arrays with numeric values, like when we create sprite arrays, like in the slider puzzle game (version 2):

static unsigned short int pieces[16][16] = {
       0x8181,0x8181,0x8181,0x8181,0x8181,0x8001,0xC003,0xFFFF},      // piece 1
       0x8181,0x8301,0x8601,0x8601,0x87E1,0x8001,0xC003,0xFFFF},      // piece 2
       0x8061,0x8061,0x8061,0x8661,0x83C1,0x8001,0xC003,0xFFFF},      // piece 3
       0x86C1,0x8CC1,0x8FF1,0x80C1,0x80C1,0x8001,0xC003,0xFFFF},      // piece 4
       0x8661,0x8061,0x8061,0x8661,0x83C1,0x8001,0xC003,0xFFFF},      // piece 5
       0x8661,0x8661,0x8661,0x8661,0x83C1,0x8001,0xC003,0xFFFF},      // piece 6
       0x8181,0x8181,0x8301,0x8301,0x8301,0x8001,0xC003,0xFFFF},      // piece 7
       0x8661,0x8661,0x8661,0x8661,0x83C1,0x8001,0xC003,0xFFFF},      // piece 8
       0x83E1,0x8061,0x8061,0x8661,0x83C1,0x8001,0xC003,0xFFFF},      // piece 9
       0x8CCD,0x8CCD,0x8CCD,0x8CCD,0x8C79,0x8001,0xC003,0xFFFF},      // piece 10
       0x8C31,0x8C31,0x8C31,0x8C31,0x8C31,0x8001,0xC003,0xFFFF},      // piece 11
       0x8C31,0x8C61,0x8CC1,0x8CC1,0x8CFD,0x8001,0xC003,0xFFFF},      // piece 12
       0x8C19,0x8C19,0x8C19,0x8D99,0x8CF1,0x8001,0xC003,0xFFFF},      // piece 13
       0x8C6D,0x8CCD,0x8CFD,0x8C0D,0x8C0D,0x8001,0xC003,0xFFFF},      // piece 14
       0x8CCD,0x8C0D,0x8C0D,0x8CCD,0x8C79,0x8001,0xC003,0xFFFF}      // piece 15

It is helpful to think of arrays such as these like tables, with rows and columns. The first [x][] set of numbers are the rows, and the [][y] set are the columns within the rows. Multidimensional arrays two levels deep like this are very common, because tables are a very useful and simple representation of many sets of data. Graphs, Maps, Matrices, and anything else that have a grid-like structure. I'm sure you can think of more.

In C, we represent arrays in sets of dimensions. Single dimension arrays are the most common. Two dimensional arrays are also very common, because there are many uses for table-like structures. I'm sure some people will have use for three dimensional arrays, but probably not many for your calculator. Arrays of more than 3 dimensions are possible, but it is unlikely you would ever need such an array.

Step 2 - Pointer Arithmetic and Multiple Indirection

Arrays of a single dimension are pointers. This is because we have a pointer to the first element, and to get all other elements, we simply look beyond this point. Multi-dimensional arrays are also pointers, we just have a special notation for handling them. For example, if you had a single dimensional array, you would have something like int array[25], and to get the 13th element, you would use array[12] (remember that numbering starts at 0, not 1). We have talked little about pointer arithmetic, so we should probably start to cover it now. Pointer arithmetic is arithmetic used for finding data relative to a pointer. However, instead of counting in units of 1 (like we normally would), 1 is equivalent to the size of the data. For example, a character takes up 1 byte, so pointer arithmetic on characters is the same as normal arithmetic. However, short integers take up 2 bytes, so adding 1 to a short integer pointer will add 2 bytes, not 1. This allows us to interact with pointers easier, since we don't have to remember how much to add to the pointer. Using our array above, we could find the 13th element of the array this way instead: array + 12. This is entirely equivalent to array[12], except for one thing. We have not dereferenced our value. This means, instead of having the value of the 13th element of the array, we have a new pointer starting at the 13th element instead of the first. To fix this, we can dereference our pointer arithmetic value, like this: *(array + 12). This statement is equivalent to array[12]. We touched upon this slightly in the sprintf program back in lesson 4.

Now, before we get into pointer arithmetic within multi-dimensional arrays, it's necessary to understand the concept of a pointer to a pointer. Multi-dimensional arrays are pointers to pointers. So, a two dimensional array is a pointer to a pointer. A three dimensional array is a pointer to a pointer to a pointer. You get the idea. Just add a pointer for each dimension. So, what are pointers to pointers? Well, they're a way of defining sets of data. Each pointer represents a dimension in the array. The top-most pointer encompasses the top-most dimension of an array. For example, if we had a three-dimensional array, short int arr3d[5][7][20], then this is similar to ***arr3d, and arr3d is the top-most level or the first dimension of the array. It has 5 sets of (7 * 20) short integers. The second dimension has 7 sets of 20 short integers, and the third dimension has 20 short integers. Now, to find the second and third dimension, using pointers, we must use multiple indirections, or dereferences. Remember from lesson 5, that to find the value of a pointer, we must dereference its value. This is known as indirection, because we are indirectly using the pointer to find a value based on the pointer. This is in contrast to direct values, which are normal variables. So, to use multiple indirection, simply add another *. So, if we were to dereference the arr3d pointer, we would get *arr3d, which is a pointer to the second dimension. **arr3d is the third dimension of our three-dimensional array. And ***arr3d is the dereferenced value of the first item in the third dimension of the first item in the second dimension of the first item in the third dimension. Easy, no? Well, it gets easier with time and practice.

Pointer arithmetic is a little different for multi-dimension arrays than for single-dimensions. It's harder. Okay, I know that's probably no big revelation, but don't be discouraged, it's not so difficult once you get the basic concept down. Remember that array + n is the (n + 1) element of an array. It is similar for multi-dimension arrays, but with a twist. We must add the values of all the secondary dimensions. So, for a two-dimensional array, like short int arr2d[5][20], if we add 1 to the arr2d pointer, we would get &arr2d[1][0], which is a pointer to the 20th element in the array, or the first element of the second set, if you prefer. This may not have been exactly what you were looking for, especially if you were looking for the 2nd element in the first set. To get this, we need to use our knowledge of multi-dimensional arrays as pointers to pointers. If we were to find a pointer to the first set, we could then add 1 to that pointer to find the value we actually were looking for. So, we can do this instead (*arr2d)+1. The (*arr2d) gives us the pointer to the second dimension. Remember, this is not a value, because a two-dimensional array is a pointer to a pointer. This is an important distinction to keep in mind. * indirection does not give a value necessarily, it gives the value that we are a pointer to. This value could be a pointer as well. So, arr2d[0][1] is the same as *((*arr2d)+1), or, the indirection of the pointer to the second dimension plus 1.

This may seem complicated now, but it will get easier with practice. Now, what about three-dimensional arrays. Well, these are like two-dimensional arrays, but we need to understand that there are three pointers involved here. So, to get arr3d[0][0][1], we would need to use *((**arr3d)+1). Okay, what was that? Easy, the multiple indirection (**) we used gave us a pointer to the third dimension, so, adding this pointer by one gave us the address of the second element, and the final indirection gave us the value. Let's add some complexity now. What would be the value arr3d[6][18][233], for a three dimensional array defined as long int arr3d[10][20][400]? Easy. *(x) = the value, but what does our x value equal? Well, we need to find the value of each pointer, and add those values together. So, we need to find the 7th plane, the 19th row, and the 233rd column. Then we simply add the values together. ((arr3d+6) + (*arr3d)+18 + (**arr3d)+233) becomes our x value. This will give us a pointer in the third dimension. The combination of all these values says that arr3d[6][18][233] = *((*((*(arr3d + 6)) + 18)) + 233). I think you can see why array notation is favored.

Step 3 - Pointer Arithmetic using Sprites

Start TIGCC and create a new project. Create a new C Source File named ptrmath. Modify the file so that it looks like this:


#include <tigcclib.h>

enum Dimensions {WIDTH = 32, HEIGHT = 78};

unsigned long int graph[5 * HEIGHT] = {

unsigned long int *graph2d[5] = {graph, graph+HEIGHT, graph+(2 * HEIGHT),
                 graph+(3 * HEIGHT),graph+(4 * HEIGHT)};

// these two functions accomplish the exact same thing
void draw(void) {
    short int loop;

    for (loop = 0; loop < 5; loop++) {
        Sprite32(loop*32,22,HEIGHT,graph+(loop * HEIGHT),LCD_MEM,SPRT_XOR);

void draw2d(void) {
    short int loop;

    for (loop = 0; loop < 5; loop++) {

void drawMenu(void) {
    // setup the medium font

    // clear the screen

    // draw the menu options
    DrawStr(0,0,"F1: Draw/Erase 1D Graph",A_NORMAL);
    DrawStr(0,8,"F2: Draw/Erase 2D Graph",A_NORMAL);
    DrawStr(0,16,"ESC: Exit the Program",A_NORMAL);

void _main(void) {
    short int done = 0, key;


    while (!done) {
        key = ngetchx();

        if (key == KEY_ESC) {
            done = 1;
        } else if (key == KEY_F1) {
        } else if (key == KEY_F2) {

Step 3a - Compile and Run the Program

Save the project and build it. Send it to TiEmu and it will look something like this:

TI-89 AMS 2.05 ptrmath.89z TI-92+ AMS 2.05 ptrmath.9xz

Step 3b - Program Analysis

Based on what we have learned so far, this should be a very simplistic program. We use a single sprite definition, but define it two ways. The first way is a single dimension array, and the second is a two-dimension array. This lets us use work with pointer arithmetic of single and multiple indirections.

Since the sprite is the same sprite, it should not come as a surprise to you that drawing the 2-dimension version is the same as drawing the single-dimension version. Since they are drawn using XOR, drawing one will cancel out the other, as well as drawing the same one twice, just like any XORed sprite.

Before we get down to the main() method, let's take a look at the sprite definition.

unsigned long int graph[5 * HEIGHT] = {

I won't copy the entire sprite. This is the key line to notice. The sprite is an unsigned long integer array of one dimension. It has 5 * HEIGHT (or 5 * 78 = 390) members. The total size of this array then is 1560 bytes, because that's 390 * sizeof(unsigned long int) = 390 * 4 = 1560. Now, to draw this sprite, we need to draw it in 5 pieces, because it's 160 pixels wide (32 * 5). Since the widest sprite we can draw is 32 pixels, we have to break it up into 5 sets, hence our graph being defined in multiples of 5.

Now, to find each set of the sprite, we will user pointer arithmetic. Since we need a pointer to use the spriteXX() functions, pointer arithmetic is the perfect choice. So, graph + (0 * HEIGHT) is our first tile, then graph + (1 * HEIGHT), etc. As you will soon see.

I think the drawMenu() and the loop is fairly straightforward, so I will skip looking over those. It is the drawing functions that we should be interested in here. So, let's start with draw().

void draw(void) {
	short int loop;
	for (loop = 0; loop < 5; loop++) {
		Sprite32(loop*32,22,HEIGHT,graph+(loop * HEIGHT),LCD_MEM,SPRT_XOR);

The loop is rather simple, we call the Sprite32() function 5 times. As we've used the SpriteXX functions before, they should be no problem for you, but let's recap real quick. The first parameter is the x coordinate, then y, then the sprite height. The important one is our 4th parameter, the sprite pointer. This is where our pointer arithmetic comes in. Final two parameters are the LCD address and the sprite mode, which is XOR.

So, what about that pointer arithmetic? Is it simple enough for you? Well, we start with our pointer (remember this is the one-dimensional array). We add the value loop * HEIGHT to get one of the 5 sets of data associated with our sprite. Because each sprite is 78 pixels tall, we have 78 long integers that define the rows. Remember, since we did not use the dereferencing operator (*), this is still a pointer, not a value, so the Sprite32() function accepts it as-is.

The two-dimensional version isn't much more complicated, but let's take a look at the sprite definition first.

unsigned long int *graph2d[5] = {graph, graph+HEIGHT, graph+(2 * HEIGHT),
				 graph+(3 * HEIGHT),graph+(4 * HEIGHT)};

Instead of redefining the data again, we used an array of pointers, which is the same thing as a multi-dimensional array. Since we have a pointer to a set of pointers, it's a two-dimensional array. The first set is just the graph pointer, which is the same as the graph array, only this time, we are a pointer to this pointer, so, to get the graph pointer from the graph2d, we would have to dereference the first element. The other patterns are similar, we just add a multiple of the height to each one up from the previous one. This gives us the 5 sets that is our graphic. So, let's take a look at the draw2d() function.

void draw2d(void) {
	short int loop;
	for (loop = 0; loop < 5; loop++) {

As you can see, it's very similar to the other function, with one key difference, we don't have to add a multiple of the height. We simply add our loop variable to the pointer, and dereference the pointer to get the other pointer, which is what we want. You'll note that we could have accomplished the same thing by doing graph2d[loop], because array notation does the first dereference for us.

Continue the Lesson in Part II


Copyright © 1998-2007 Techno-Plaza
All Rights Reserved Unless Otherwise Noted

Get Firefox!    Valid HTML 4.01!    Made with jEdit