Pointers and References – Part 2 (Intermediate Stuff)

This is my second posting on pointers and references. In part 1 you could read about the basic nature of pointers. Now we will have a look on two very common examples of how pointers are being used.

1. Passing large data structures to functions

If a large amount of information is passed to functions (methods) by value, this consumes a huge amount of stack memory. So passing addresses which refer to a place where the actual information is being found is more efficient. If you want to pass a text string (array of chars in C) to a function, you might want to use a char pointer which refers to the first character of the text string. All other characters of the text string will follow subsequently; the ASCII zero character (EOT) indicates the end of the text string.

void function( char* text ) {
  // dereferencing and using text
  ...
}

If you call the function above, you just need to pass the address of a character array.

function( &text ); // where text is declared as char text[]

Yet another example of large data types in C are structures. These should always be passed by reference, too.

Java is a programming language for those who don’t want to think about the nuts and bolts of memory management. So it automatically wraps all large data types into objects. These objects are always handled by reference.

So if a method is defined as

public void method( String text ) {
  // using text
}

its parameter is a reference, even though the programmer won’t notice this fact on the first glance. In fact, he will only notice its reference-like behavior in specific situations, for example if he tries to compare two text strings by ==. This operator compares the value of the two string references, which (in most cases) has nothing to do with the content of the strings.

2. List programming

Using pointers can come in handy when working with lists, because there is a feature called pointer arithmetic. This means that you can increment or decrement a pointer in order to have it pointing to the next (or to the previous) storage address. Consider the following snippet of C code:

int a[] = { 3, 4, 5, 6 };
int* a_ptr = a;
a_ptr++;
printf( "%d\n", *a_ptr );

a_ptr is declared to be a pointer to int, but we actually assign to it the address of an array of int values a. Since an array variable always contains the address of the array instead of its values, we don’t need any & operator in the second line. In the third line of code the pointer a_ptr is incremented. It is now pointing to the next address in storage, that is those of the second element of the array (4).

What happened here is that we accessed an array element by reference instead by its index. It is crucial for the compiler to know the size of whatever datatype a_ptr may be pointing to. If you increment an int-pointer it will increment by the size of int (which may be 4 byte on your PC and 2 byte on a 8051 microcontroller). Other datatypes will be handeld respectively.

Cool stuff, eh? This way you could do linked lists, binary trees, whatever fancy things you like.

This is a great opportunity, however, to come to the other side of the coin when working with pointers.

  • What would happen if you changed a_ptr++ to a_ptr– in the code snippet above? (What’s in the memory before the array?)
  • What would happen if you declared a_ptr to be a pointer to char and you add an explicite type cast to assign the address of the int-array to it? (It surely would be incremented by the size of char …)

I’ll leave the answers to you. Get a C-Compiler (comes with every Linux Live CD) and have a try. You’ll understand in an instant why pointers can be evil and why there is no such thing in Java. 😉

In my next posting, I’m going to show you some of my kinky (but mostly useless) pointer gimmicks I sometimes use in my lectures to impress my students. Of course there is no magic about it, but it’s great to be admired by the unenlightened. 😉

See you in my next lecture!

— Andre M. Maier

About bitjunkie

Teacher, Lecturer, and BITJUNKIE ...
This entry was posted in Pointers, Programming Essentials, Uncategorized and tagged , , , . Bookmark the permalink.

Leave a comment