Emphasizing The Tie-up of C Pointers and Arrays

Introducing pointers before arrays in a C course

All the C courses and books that I have seen, introduce arrays early and pointers later, sometimes much later. Pointers have the reputation of being difficult, and this postponing makes it worse. Further, the C language ties up the concepts and pointers and arrays in an essential manner, but it does not get imprinted onto the students’ mind because of this delay. To emphasize this tie-up and to give students a longer exposure of pointers, I decided to introduce pointers right in the beginning, just before arrays which come quite early. Here is a summary of the new flow of concepts, which takes about two to three classroom hours:

Like the other types such as double, int, char, etc. which store different types of information, we have one more type: the pointer type. The variables of this type can store addresses of memory locations. Otherwise, these variables are just like other variables: they can be assigned values of appropriate types, they can be passed to functions or returned by functions, they can take part in their own arithmetic (which is different from the integer arithmetic or the floating point arithmetic, and will be covered later), etc. The pointer type is further qualified by the type of data it can point to: For example, a pointer of type int * is taken to contain address of an integer. In addition, there is a generic type of pointer, void *, which is pointer to no specific type (so it can be used to point to anything in general).

There are ways to create values of this type: the address-of (&) operator is one common way. The expression &x has a value that represents the address of a variable x, and can be assigned to a pointer variable of the corresponding type. There are ways to use address-values: a common way is the dereferencing (*) operator. If p is a pointer of type int *, then the expression *p represents the integer that is located at the address that p contains.

And now come the arrays: An array, say of 10 integers, is declared as

int k[10];

This declaration makes k an array of 10 ints, only by

  • reserving a contiguous space of 10 elements
  • making k a constant whose value is the address of the first element

Then comes the turn for pointer arithmetic, for accessing the elements: When we know we have 10 elements placed contiguously and we have k as the address of the first, k+i represents the address of the ith element (counting from zero). Thus i acts as the offset. The expression k+i means the address of ith element irrespective of the type and the size of the array elements by the definition of the language. Since k+i is the address of ith element, the actual element is obtained by dereferencing it: *(k+i). Now since arrays are so common, the C standard has provided another convenient notation, k[i], which is exactly equivalent to *(k+i). And this is true even when you have a pointer variable in place of k. Pointer arithmetic makes it clear why arrays in C have 0 as the index for the first element. Further, this is a good time when passing of arrays as arguments to functions can be easily understood simply as passing of pointers.

I am curious to know whether such an approach has been tried anywhere else, and their viewpoints.

Advertisements

2 comments

  1. Yes, you are absolutely right saying this. I had some fellows coming from IIT Madras, doing their Masters in Aerospace Engineering. I happened to see their codes, my god one could imagine how afraid they were from using pointers !!!!!!
    Its true that the approach used to teach C everywhere (it was not there in CMS !!!), is a bit derogatory in a sense these topics (pointers) are getting delayed and delayed and finally they literally get delayed.

  2. aparna natu (phadke) · · Reply

    yes, u r right. Pointer is a type of variable should be emphasized first while teaching. then it is easy to apply to anywhere in the programme ,not only arrays or so.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: