Introduction to C Programming - Part 6

by Björn Fahller

An Introduction to C Programming - Part 6
As promised last month, we're now going to take a look at pointers and arrays. If you search for books in a library, you get in contact with real-world pointers and arrays, in many ways. When searching, you wade through a collection of pointers to books, be they stored on CD-ROM, micro film or card files. The information you find, is not the book itself (of course,) but a pointer to the book. The pointer says where you can find it. When you know where to find it, you go to the bookshelf where it's supposed to be. The bookshelf is an array of books, i.e. one entity holding several items of the same kind. Pointers and arrays conceptually have one thing in common; their type is defined by what they refer to. In the library we have pointers to books and arrays of books.

In this month's lesson, I will first explain the basics of pointers, and then arrays. After this, I will show you how they're both tightly coupled in C.

Pointers
As mentioned in the short introduction, the type of a pointer is defined by the type it points to. An example will show you a few declarations of pointers: int* pint;        /* pointer to int */ book *bookPointer; /* pointer to book */ It is the "*" that makes it a pointer declaration.

Note the slight difference in the declarations above with respect to the placement of the "*". This makes no difference at all to the C compiler, but is a matter of taste and programming style. I will not enter a taste-war here, but there are good arguments for and against both styles. The ones I've heard of are:
 * 1) The "*" is what makes it a pointer type. Hence it's a part of the type declaration, so obviously it should be written like the first alternative.
 * OK, the "*" is what makes it a pointer type, but the way C interprets variable declarations makes the above style dangerous if you declare several variables at the same time.

To explain point 2, have a look at this: int* p1, p2, p3; What do you think this declares? It declares "p1" as a pointer to "int", and "p2" and "p3" as "int." Most probably, this was not what was intended.

If you decided that style 1 is your preference, be very careful in always declaring one variable at the time, and not several in the same declaration.

Pointer Initialization and Dereferencing
A pointer is not of much use if it doesn't point to anything. How, then, do we get a pointer to point to something? We can get the pointer to any variable, by prepending its name with "&", which in this case is called the "address of" operator. For example: A pointer is still of little use, if we cannot reach what the pointer is pointing to. Doing this is called "dereferencing" and is done by prepending the name with "*", or the "dereference" operator. Now we can actually do something: The output of the above example is: *pint = 2, anInt = 2 *pint = 5, anInt = 5 The first line tells us that we can get the value of "anInt" by dereferencing "pint", since "pint" points to "anInt." The second line tells us that we can not only get the value of "anInt" through "pint", but also change its value.

Pointer Idioms in Function Calls
While it may not seem like it, this is useful. A fairly common idiom in C, is to declare functions like this: int readDouble(double* pValue); Where the return value of the function is an error indicator, and the double read is stored in the variable pointed to by the parameter. An example of use can be this: Here the while loop goes on, begging for a floating point number as long as "readDouble" returns 0.

This technique is used very often in the OS/2 API, where almost all functions return an error code.

Pointer to Struct
A pointer can point to any type, including struct and enum types, and even pointer types. For various reasons, pointers to struct types are very common. Here is an example of how you can deal with them: The last line does a number of things at once. First, within the parenthesis, the pointer is dereferenced. The dereferenced value is the struct, to which we pick the "a" component, and assign it the value 5. Because of the precedence rules of "." and unary "*", the parenthesis are unfortunately necessary. However, accessing components of struct types through pointers is so common that there is a special operator "->" for it. The last line of the above example can be rewritten as: structPointer->a = 5; Now, here follows a small example program, that makes use of all you have so far learned about pointers. This program includes a "readDouble" as mentioned above, that does not alter the value of the pointed to variable if the read fails. It includes a struct type called "Coordinate", and of course a "readCoordinate" function. A test run might look like follows: This will be enough with pointers for a while. Off to...

Arrays
Arrays is a way of creating a multi-part type, just like structs are, but with arrays, the elements are indexed by an int instead of having individual names, and all elements have the same type. Here's an example of declaring an array of 20 integers: int intarray[20];

Here "intarray" is a variable storing 20 elements, of type int. The number of elements must be a constant known at compile time, so it is not possible to do this:

Array Indexing
The elements of an array are reached with the subscript operator "[x]", where x is an "int." The indexes start with 0, so the first element of an array is always "arrayname[0]". The index of the last element is always one less than the number of elements. So, in the example "intarray" above, valid indexes are from 0 to 19 inclusively. An example printing the 10 first elements of the Fibonacci series in reversed order shows how the indexing is done: It is very important to be careful with the index values. C compilers normally do not check if the index values are valid. The error showed below will not be caught by the compiler, will probably result in odd run-time behaviour that is likely to give you many days of debugging work. double darray[10]; darray[10] = 3.141592; Unfortunately, this error, indexing one past the end, is very common, because it's so easy to make unless you're careful. In this case, where the index is a numeric constant, it's fairly easy to see, but when the index is a variable coming from user input, a return value from a function, or something such, it's not quite as easy to see.

Array Initialization
Variables can be initialized when created, as you've seen, among others, in the pointer examples above: int anInt = 2; /* initialize anInt to have the value 2 */ int* pint = &anInt; /* initialize pint to point to anInt */ Arrays too can be initialized when created, and you do that as follows: int anarray[5] = { 1, 3, 5, 7, 11 }; This causes "anarray[0]" to be initialized with the value 1, "anarray[1]" to be initialized with the value 3, and so on until "anarray[4]" which is initialized with the value 11.

If, like in the fibonacci example, you only know what to initialize the first few elements with, you can initialize just them, like this: int fib[10] = {1, 1 }; Here "fib[0]" and "fib[1]" are both initialized with the value 1, and the other 8 elements are uninitialized.

One bothering thing with initialising a whole array, is that you must be careful to make the array the same size as the number of elements you initialize it with. It's always a problem when you store the same information, in this case the number of elements, in two or more places, because sooner or later you will make a change to one of them and forget the other. With array initialization there is a short-cut: int anarray[] = { 1, 3, 5, 7, 11 }; Now the number of elements will be defined by the number of values are entered in the initializer list.

The problem with this, is that you cannot easily see how many elements the array holds. If you, in several places in your program, use 5 as the number of elements, you have the same problem anyway. There is a compile time operator called "sizeof", probably not really intended for this problem, that solves it rather nicely. "sizeof" looks like a function, to which you pass either a variable or a type. When compiling, the "sizeof" operator with its argument, is replaced by the number of bytes the type or variable occupies. Since all elements of an array are of the same type, and hence occupy the same number of bytes, the compiler can calculate the number of elements for us: int anarray[] = { 1, 3, 5, 7, 11, 13 }; unsigned arrayElements = sizeof(anarray)/sizeof(anarray[0]); Here "arrayElements" will be 6, and if "anarray" is extended or shrunk, or if the type is changed to, say, array of short, "arrayElements" will still correctly tell the number of elements in "anarray."

Strings
A character string in C is an array. They're arrays of char, with the special characteristic that the last element has the value 0. As you have seen numerous times, a string is initialized with the characters between quotation marks. The terminating 0 is not visible, but is there none the less. The two examples below are identical, except of course for the name of the identifiers: char string1[] = "string"; char string2[] = {'s', 't', 'r', 'i', 'n', 'g', 0}; It is important to remember the terminating 0, and the space needed for it. Just about all string handling functions rely on strings ending with 0.

Note that if you declare the string with a size, it will have that size, even if that means the terminating 0 will be stripped. char hello[5] = "hello"; /* string without terminating 0. */ If you ever find a reason to make a declaration like the above, I think you really should comment that the stripping of the terminating 0 is intentional, and why. Other developers reading your code will otherwise very probably think of it as an error. Note that if you use a C++ compiler, this construction is an error.

Arrays and Functions
As with any other type, it is of course possible to pass arrays to functions. Unfortunately in doing so, the size of the array is lost, so the size must be passed separately unless you have some other way of finding it out. Below is the fibonacci example again, but making use of initialization, sizeof and a function printing the reversed series: Except from the size information being lost when passing an array to a function, there is something else that is a bit special. This example will show you what: Running this gives the following output: D:> ftest int 1, int* 35, struct 10, int[] 105 As expected "integer1" was not changed, since only the value that "integer1" had was passed in the function call, not the variable itself. "integer2" was given the value 35, since the pointer to it was passed to the function, and it changed whatever the pointer pointed to. The struct component "a" didn't change, but "array[0]" did. It appears that arrays behave just like pointers do. In fact, when passing an array in a function call, what is passed is always a pointer to the first element of the array.

Arrays and Pointers
So, arrays are passed to functions as a pointer to the first element of the array. How is that possible? We can, after all, use the subscript operator "[x]" on the array when in the function.

The answer is that there is a lot more to pointers than what I've told you so far. A pointer is a type, which is not just restricted to pointing to things, but which it is possible to do arithmetic on. You can add an integer value to a pointer, and get another pointer value from that. That is exactly what the subscript operator does. Take a look at this: The output of this program is: 2, 3, 5, 7, 11 What happens is that "pint" points to the first element, and dereferencing it will give the value of the first element. "pint+1" will point to the second element, and thus "*(pint+1)" will give the value of the second element, and so on.

Pointers can be manipulated with operators "++", "--", "+=", "-=", and can be compared. When reading C code, you will often find constructions like: The output of this program is: How does this work? "pint" is first set to point to the third element of "array", and the 4th element from there, i.e. the 7th element or "array", which is 13, is printed. In the for loop, "pint" is first set to point to the first element of "array". Since "pint" is dereferenced in the printout, the first value of the array is printed. Then "pint", which still points to the first value of "array" is compared if it is less than "array+elements", which it is, since "array+elements" is one off the end of "array". Since it was smaller, it is incremented to point to the second element, and then the loop goes on, until the last element of the array has been printed.

Here I've only used pointers to int, but the behaviour is exactly the same regardless of the type pointed to.

I think this will be enough for this month.

Recap

 * With the aid of pointers, we can reach other variables, to read and alter their values.
 * We get a pointer to a variable with the unary operator "&".
 * Pointers can be dereferenced with unary "*", or "->" if they point to a struct.
 * Pointers can be used to let a function both leave a result and return an error code.
 * Arrays is a way of storing several components of the same type.
 * The elements in an array are indexed by an integer using the subscript operator "[x]".
 * Pointers and arrays are tightly connected, and when passing an array in a function call, a pointer to the first element is what really is passed.
 * If a pointer points to an element in an array, the subscript operator can be used on it, as if it was an array.
 * It is possible to do arithmetic on pointers, and to compare their values.

Coming Up
While it may not seem like it, most of the C language has now been covered. What's lacking are a few details, and of course an explanation of at least some of the numerous functions in the ANSI C library, and the OS/2 API. To prove that this really is true, I'm going to spend the next few articles on writing a real program. As it develops, I'll explain the functions, and the few yet uncovered C constructions used. From this you will learn, not just C, but also some good software engineering practices.

Please don't hesitate to e-mail me if you have questions, wishes for details to cover or want me to clarify things.