Using Pointers

Pointers in C, if used properly, give way to faster programs and efficient code. But if they are misused, could lead to huge programs crashing, without as much as an iota of warning.

Pointer Manipulation

Complicated manipulation of pointers is possible through the use of the address and the dereferencing operators and also by combining them with relational operators '+ and'. Now observe the following code segment

  1. int x=1, y=2;
  2. int *xptr, *yptr;
  3. xptr = &x; /* xptr and yptr are pointing to x add y respectively */
  4. yptr = &y;
  5. y = *yptr + 1; /* get what yptr is pointing to, add one to it and assign the result to y */
  6. yptr = xptr /* yptr is also pointing to x now */
  7. yptr = &y; /* the address of y is again assigned to yptr */
  8. *yptr = *xptr; /* yptr is made equal to whatever xptr is pointing to */

Note that in the above code segment, lines 6 and 8 are not equal. In line 6 yptr is made to point to whatever ptr is pointing to, y remains the same. Whereas in line 8, the value of what yptr is pointing to is made equal to that of what xptr is pointing to. The value of y is automatically changed. It is made equal to one.

Pointers and Arrays

Pointers and Arrays have always been closely related to each other, so closely that any operation that could be performed by array subscripting can also be achieved by using pointers. The declaration

char a[5];

defines an array a of size 5, and implies that five consecutive bytes of memory have to be allocated and set aside for it.

a[0] a[1] a[2] a[3] a[4]

The notation a[i] refers to the ith clement from the starting position of the array. If aptr Is a pointer to character declared as char *aptr; then the assignment aptr = &a[0]; assigns the address of the first element of the array a to aptr.

Now the assignment
char x;
x = *aptr;

will copy the contents of a[0] into x.

If aptr points to the first element in the array, then aptr + 1 (which is the same as a[1]) points to the next element, aptr + i points i elements after aptr, and so on. Thus if aptr points to a[0], then

*(aptr + 1)

refers to the contents of a[1] . aptr [1] is the address of a[i], so

refers io the contents of a[i].

There are subtle differences between a pointer declaration and an array declaration, In the declaration int a[5], *aptr; both a and aptr are pointers to int. But as soon as a is declared, memory is set apart for it whereas it is not so in the case of aptr. It has to be specifically made to point to some location in memory, or else if something has to be stored in it, memory has to be specifically allocated for it. In the above declaration, both a and aptr are pointers, but a is a pointer with a fixed address (here, the statement a=a+1 is not a valid statement), whereas aptr is not so (the statement aptr=aptr+1 is a valid one).

Pointers and Two-Dimensional Arrays

The declaration char *char ptr(5]; /* array of pointers */ defines five pointers, each of which is pointing to a character. Since one pointer can be used to represent a string, a set of strings can be represented in the above manner. For the above declaration, initialization can be done as follows

char_ptr [1] = "String1";
char_ptr (2] = "String2";
char_ptr (3] = "String3";

Note that the array of pointers declared above is different from a two-dimensional array.

While with the declaration

char str_ptr [5] [10];

memory is definitely allocated for 10 * 5, 50 bytes, for an array of pointers, the definition only allocates five pointers. An important advantage of this array of pointers is that each row may be of different lengths.