Introduction to Pointers in C

in programming •  2 months ago 

If you want to be proficient in the writing of code in the C programming language, you must have a thorough working knowledge of how to use pointers. Unfortunately, C pointers appear to represent a stumbling block to newcomers, particularly those coming from other computer languages such as JavaScript or Python.

What is a Pointer?

One of those things beginners in C find difficult is the concept of pointers, and - probably - one of the causes of this, is because newcomers have a weak or minimal knowledge about variables (as they are used in C). Then, let's start by talking about variables in general.

A variable in a program is something with a name, the value of which can vary. The compiler and linker handle this as a specific block of memory within the computer to hold the value of that variable. The size of that block depends on the range over which the variable is allowed to vary. For example, on computers, the size of an integer variable is 4 bytes, and that of a long integer is 8. In C the size of a variable type, such as integer, doesn't need to be the same on all types of machines.

When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name z by writing:

int z;

On seeing the "int" part of this statement, the compiler sets aside 4 bytes of memory to hold the value of the integer. It also sets up a symbol table. In that table, it adds the symbol z and the relative address in memory where those 4 bytes were set aside.

Thus, later if we write:

z = 2;

We expect that at runtime when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of z.

In a sense, there are two "values" associated with the z variable. One is the value of the integer stored there (2 in the above example) and the other one, the "value" of the memory location, i.e. the address of z. Some texts refer to these two values with the name of rvalue and lvalue, respectively.

In some languages, the lvalue is the value permitted on the left side of the assignment operator "=" (.i.e. the address where the result of the evaluation of the right side ends up), The rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues cannot be used on the left side of the assignment. Thus 2 = z; is illegal.

Consider the following:

x = 7;
y = x;

In the above example, the compiler interprets the x in line 1 as the address of the variable x (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the x is interpreted as its rvalue (since it is on the right-hand side of the assignment operator '='). That is, here the x refers to the value stored at the memory location set aside for x, in this case, 7. So, the 7 is copied to the address designated by the lvalue of y.

In all of these examples, we are using 4-byte integers so all copying of rvalues from one storage location to the other is done by copying 4 bytes. If we would have been using long integers, we would be copying 8 bytes.

Now, let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). The size required to hold such a value depends on the system. It is usually 4 bytes. The actual size required is not too important as long as we have a way of informing the compiler that we want to store an address.

Such type of variable is called a pointer variable (for reasons which hopefully will become clearer a little later). In C, when we define a pointer variable, we do so by preceding its name with an asterisk. In C we also give our pointer a type which, in this case, refers to the type of data stored at the address we will be storing in our pointer. For example, consider the following variable declaration:

int *ptr;

ptr is the name of our variable (just as x, z and y were before). The '*' informs the compiler that we want a pointer variable, i.e. to set aside how many bytes are required to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer. Such a pointer is said to "point to" an integer. However, note that when we wrote int z; we did not give z a value. If this definition is made outside of any function, ANSI compliant compilers will initialise it to zero. Similarly, ptr has no value, that is, we haven't stored an address in it. In this case, again, if the declaration is outside of any function, it is initialised to a value in such a way that it is guaranteed to not point to any C variable or function. A pointer initialised in this manner is called a "null" pointer.

The actual bit pattern used for a null pointer may or may not evaluate to zero as it depends on the specific system on which the code is developed. To make source code compatible between various compilers and various systems, a macro is used to represent a null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL macro, as with an assignment such as ptr = NULL, guarantees that the pointer has become a null pointer. Similarly, just as we can test for an integer value of zero, as in if (x == 0), we can test for a null pointer using if (ptr == NULL).

But, back to using our new variable ptr. Suppose now that we want to store in ptr address of our integer value x. To do this, we have to use the unary & operator and write:

ptr = &x;

What the & operator does is retrieve the lvalue (address) of x, even though x is on the right-hand side of the assignment operator '=', and copies that to the contents of our pointer ptr. Now, ptr is said to "point to" x. Now, there is only one more operator we need to discuss.

The "dereferencing operator" is the asterisk and it is used as follows:

*ptr = 7;

That previous statement will copy 7 to the address pointed to by ptr. Thus, if ptr "points to" (contains the address of) x, the above statement will set the value of x to 7. That is, when we use the '*' this way, we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.

Similarly, we could write:

printf ("%d\n", *ptr);

to print to the screen the integer value stored at the address pointed to by ptr.

One way to see how all this stuff fits together, would be to run the following program and then review the code and the output carefully:

int j, k;
int *ptr;

main (int argc, char **argv)
  j = 1;
  k = 2;

  ptr = &k
  printf ("j has the value %d and is stored at %p\n", j, &j);
  printf ("k has the value of %d and is stored at %p\n", k, &k);
  printf ("ptr has the value %p and is stored at %p\n", ptr, &ptr);
  printf ("the value of the integer pointed to by ptr is %d\n", *ptr);

  return 0;

Pointer types and arrays

Let's consider why we need to identify the type of the variable that a pointer points to, as in:

int *ptr;

One reason for doing this is so that later, once ptr "points to" something, if we write:

*ptr = 2;

The compiler will know how many bytes to copy into that memory location pointed to by ptr. If ptr was declared as pointing to an integer, 4 bytes would be copied, similarly for floats and doubles. Defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting of then integers in a row. That is, 40 bytes of memory are set aside to hold 10 integers.

Now, let's say we point our integer pointer ptr at the first of these integers. Furthermore, let's say that the integer is located at memory location 100 (decimal). What happens when we write:

ptr + 1;

Because the compiler knows this is a pointer and that it points to an integer, it adds 4 to ptr instead of 1, so the pointer "points to" the next integer, at memory location 104. Similarly, where the ptr is declared as a pointer to a long, it would add 8 bytes to it instead of 1. The same goes for other data types such as floats, doubles, or even user-defined data such as structures. This is obviously not the kind of "addition" that we normally think of. In C it is referred to as addition using "pointer arithmetic", a term which we will come back to later.

Similarly, since ++ptr and ptr++ are both equivalents to ptr + 1 (though the point in the program when ptr is incremented may be different), incrementing a pointer using the unary ++ operator, either pre or post, increments the address it stores by the amount sizeof (type) where "type" is the type of the object pointed to.

Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers.

Consider the following:

int my_array[] = { 1, 23, 17, 4, -5, 100 };

Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using mu_array[0] through my_array[5]. But, we could alternatively access them via a pointer as follows.

int *ptr;
ptr = &my_array[0]; // points our pointer at the first integer in our array

And then we could print out our array either using the array notation or by dereferencing our pointer. The following code represents this:

#include <stdio.h>

int my_array[] = { 1, 23, 17, 4, -5, 100 };
int *ptr;

main (int argc, char **argv)
  int i;
  ptr = &my_array[0];

  for (i = 0; i < 6; i++)
        printf ("my_array[%d] = %d\n", i, my_array[i]);
        printf ("ptr + %d = %d\n", i, *(ptr + i));

  return 0;

If you compile and run the above program and carefully note the lines in between the for loop, you'll note that the program prints out the same values in either case. Also, observe how we dereferenced our pointer, we first added i to it and then dereferenced the new pointer.

In C, the standard states that wherever we migh use &var_name[0], we can replace that with var_name, thus in our code where we wrote:

ptr = &my_array[0];

we can write:

ptr = my_array;

to achieve the same result.

Void pointers

AS we have seen, we can have pointers of various types. So far we have discussed pointers to integers and pointers to characters. In coming entries, we will be learning about pointers to structures and even pointers to pointers.

Also, we have learned that on different systems the size of a pointer can vary. As it turns out, it is also possible that the size of a pointer can vary depending on the data type of the object to which it points. Thus, as with integers where you can run into trouble to assign a long integer to a variable of type short integer, you can run into trouble attempting to assign the values of pointers of various types to pointer variables of other types.

To minimise this problem, C provides for a pointer of type void. We can declare such pointer by writing:

void *void_ptr;

A void pointer is some sort of a generic pointer. For example, while C cannot permit the comparison of a pointer of type integer with a pointer of a type character, for example, either of these can be compared to a void pointer. Of course, as with other variables, casts can be used to convert from one type of pointer to another under the proper circumstances.

This was a lot of information, I hope you understand the biggest part of it, it is completely normal to not understand pointers (which is such a complex and broad programming topic) with just an article you found on the web, search for more information, examples, do more research about it, pointers are completely necessary to master the C programming language and even to understand languages that don't make use of pointers (as you learn a lot about how variables work, how they are stored in memory and how they are understood by the processor).

I have planned more entries about this, such as strings and much, much more.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!