Pointers in K&R

By Susam Pal on 05 Sep 2020

I learnt C from the book The C Programming Language, 2nd ed. (K&R) written by Brian Kernighan and Dennis Ritchie about 18 years ago during my engineering studies. The subject of pointers was generally believed to be scary among fellow students and many of them bought pretty fat books that were dedicated solely to the topic of pointers. However, when I reached Chapter 5 of the book , I found that it did a wonderful job at teaching pointers in merely 34 pages. The chapter opens with this sentence:

A pointer is a variable that contains the address of a variable.

The exact point at which the whole topic of pointers became crystal clear was when I encountered this sentence in § 5.3 Pointers and Arrays:

Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as *(a+i).

Indeed, it was easy to confirm that by compiling and running the following program:

#include <stdio.h>

int main() {
    int a[] = {2, 3, 5, 7, 11};
    printf("%d\n", *(a + 2));
    printf("%d\n", a[2]);
    printf("%d\n", 2[a]);
    return 0;
}

The output is:

5
5
5

C was the first serious programming language I was learning back then and at that time, I don't think I could have come across a better book than K&R to learn this subject. Like many others, I too feel that this book is a model for technical writing. I wish more technical books were written like this with clear presentation and concise treatment.

Comments