The C Programming Language - Pointers and Arrays

19 March, 2016 Comments1.46K

Unary operator (& is used to get the address of a variable. This operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants or register variables.

A pointer is constrained to point to a particular kind of object, e.g. pointer to integer, float, character etc. except pointer to void which can hold any type of pointer but cannot be de-referenced.

*ip + 1 // increments the value pointed by ip since * has higher precedence than +
++*ip // increments the value pointed by ip
(*ip)++ // increments the vlaue pointed by ip, needs parenthesis around *ip, since precedence of ++ and * is same and they are evaluated from right to left

Pointer argument enables a function to access and change objects in the caller routine. They can also be used to return back multiple set of values from to the caller (otherwise function can only return one value using return statement)

Adding 1 to a pointer points the pointer to the next object regardless of the data type. e.g.

int arr[5];
int *p;
p = &arr[0]; // p contains the address of first element of the array , let us assume 0x1000.
p + 1 // p points to the 1st element of the array now, the content of pointer would be 0x1004 considering the size of an int is 4 bytes.

An array-and-index expression is equivalent to one written as pointer and offset. e.g.

a[i] <==> *(a+i)

There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.

When an array name is passed to a function, it is the address of the first element of that array which is actually passed to the function which is nothing but a pointer. Hence, it is quite legal to do s++ in the above program and this increment would not affect the caller as 's' is now a local variable to this strlen function.

C guarantees that zero is never a valid address for data, so a return value of zero can be used to signal an abnormal event, in this case no space.

Pointers and integers are not interchangeable except 0 (zero). zero may be assigned to a pointer and pointer can be compared with constant zero. Infact NULL is a symbolic constant often used in place of zero. NULL is defined in <stdio.h>

If p and q points to the same array, then relations like ==, !=, = etc., work properly. Behavior is undefined if p and q do not point to the same array.

An integer can be added or subtracted from a pointer. increment and decrement is based on the data-type to which the pointer is pointing to. If an int is 4 bytes, the int pointer will move by 4 bytes with each increment.

Pointer subtraction is also valid. See the use in the following program:

The header <stddef.h> defines a type ptrdiff_t that is large enough to hold the signed difference of two pointer values. If we were being cautious, however, we would use size_t for the return value of strlen, to match the standard library version. size_t is the unsigned integer type returned by the sizeof operator.

Pointer arighmetic:

Pointer arithmetic is consistent: if we had been dealing with floats, the earlier version of alloc and afree could easily be reused by just modifying char to float throughout the code. All the pointer manipulations automatically take into account the size of the objects pointed to.

The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal. It is not legal to add two pointers, or to multiply or divide or shift or mask them, or to add float or double to them, or even, except for void *, to assign a pointer of one type to a pointer of another type without a cast.

A string is an array of characters. Internally, the array is terminated with the null character '\0' so that programs can find the end of the string.

C does not provide any operators for processing an entire string of characters as a unit. It has to be done character by character.

amessage is an array, just enough to hold the sequence of characters and '\0' that initializes it. Individual characters can be changed at will, but amessage will always refer to the same storage. On the other hand, pmessage is a pointer, initialized to hold address of a location where the string is stored. The pointer can be modified to point to any other location, but the result is undefined if you try to modify the string contents (because the location where this string is stored is un-modifiable)

Since pointers are variables themselves, they can be stored in arrays just as other variables can. This is known as Pointer array and has the following representation:

char *lineptr[MAXLINES];

Such a representation is neccessary when we have to store a set of variable length strings. Using pointers we can dynamically allocate only the required storage for each string and store all those pointers in an array for easy access later on. For example, if we have to sort these strings, we can use our earlier written qsort() and swap the pointers in the array with the help of strcmp() to compare the two strings.

Note that 2-dimentional array is really a 1-dimentional array with each element is yet another 1-dimentional array. To access an element in 2-d matrix use the following notation:

daytab[i][j] /* [row][col] */

Initialization is done per-row basis as shown above; each row again is initialized by a corresponding sub-list. We started the array daytab with a column of zero so that month numbers can run from the natural 1 to 12 instead of 0 to 11. Since space is not at a premium here, this is clearer than adjusting the indices.

f(int daytab[2][13]) { ... }

It could also be

f(int daytab[][13]) { ... }

since the number of rows is irrelevant, or it could be:

f(int (*daytab)[13]) { ... }

which says that the parameter is a pointer to an array of 13 integers. The parentheses are necessary since brackets [] have higher precedence than *. Without parentheses, the declaration

int *daytab[13]

is an array of 13 pointers to integers. More generally, only the first dimension (subscript) of an array is free; all the others have to be specified.

Each array element here is a pointer to char and each of the pointer is initialized as constant character string. The characters of the i-th string are placed somewhere, and a pointer to them is stored in name[i]. Since the size of the array name is not specified, the compiler counts the initializers and fills in the correct number.

Referring to an element in arrays a and b is exactly same - a[3][4] and b[3][4]. However, a has a fixed size of 200 int sized locations, but b is just an array of 10 pointers. Assuming that every element of b point to a 20-element array, the size would be 200-int sized locations + 10 extra cells for the pointers. The important advantage of the pointer array is that the rows of the array may be of different lengths. That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all.

In environments that support C, there is a way to pass command-line arguments or parameters to main function. main can be called with two arguments.

The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string.

By default, argv[0] is the name of the program itself. so argc is at least 1. Consider an example:

echo hello world

here argc will be 3, with argv[0] pointing to echo, argv[1] to hello and argv[2] to world. Additionaly, the standard requires that argv[argc] is a null pointer.

++argv is a pointer to an argument string, so (*++argv)[0] is its first character. (An alternate valid form would be **++argv.) Because [] binds tighter than * and ++, the parentheses are necessary; without them the expression would be taken as *++(argv[0]). In fact, that is what we have used in the inner loop, where the task is to walk along a specific argument string. In the inner loop, the expression *++argv[0] increments the pointer argv[0] (a particular string)

In C, a function itself is not a variable, but it is possible to define pointers to functions, which can be assigned, placed in arrays, passed to functions, returned by functions, and so on.

int (*comp)(void*, void*) : comp is a pointer to function that accepts two void * arguments and return an integer. () are neccessary here, since () has higher precedence than *. Without () the declaration becomes - int * comp(void*, void*) which declares a function comp returning a pointer to an integer.