Pointers and Arrays
Pointers
A pointer is a variable that contains the address of another variable.
Understanding pointers is crucial to understanding C, as pointers are everywhere in C.
Since a pointer contains the address of a variable, it is possible to access the variable indirectly through a pointer.
The unary operator, &, gives the address of a variable.
The operator & cn be applied only to variables and array elements.
The following statement assigns the address of the variable x to the variable px. px is said to point to x.
px = &x;
- The unary operator * accesses the value that an address points to.
- Assuming that y is an int and px the pointer to an int, the following set of statements, sets the value of y equal to the value stored in the address stored in the variable px.
y = *px;
- It is necessary to declare all of the variables involved.
- The following code creates a pointer to an integer value.
int *px;
- The syntax above is intended as a mnemonic. The combination *px is an int. It is an int wherever it appears.
- A pointer must be defined to point to only one type of variable.
- Pointers can be used in expressions.
- If px is defined as a pointer to an int, the following code adds 1 and the value pointed to by px and places that value in y.
y = *px + 1;
- Again, if px is defined as a pointer to an int, the following code adds one and address stored in px and returns the value pointed to by this new address and places that value in y.
y = *(px + 1);
- Dont worry if this is not entirely clear. Pointers will become easier to understand as you work with them more.
Pointers as Function Arguments
- C passes arguments to functions by "call by value."
- The above statement means that when C passes a value to a function, it actually makes a copy of that value and passes the copy to the function.
- On completion, a function does not return the values passed to it as arguments.
- Modifications to these variables are lost.
- This means that there is no direct way for a function to modify an ordinary argument.
- A function can only return one value.
- How then, can we modify more than one value in a function?
- The answer is pointers.
- In order for a function to modify a variable in a calling routine, the caller must provide the address of the variable to be modified.
- The following swap function, accepts two pointers as arguments, swaps the values pointed to by those two pointers, thereby swapping the two values.
swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
Pointers and Arrays
- In C, there is a strong relationship between pointers and arrays.
- Pointers are, in general faster than arrays. However, to novice programmers, they are often more difficult to comprehend.
- The following declaration declares an array of ten ints, stored in ten consecutive addresses.
int arrayOfTen[10];
- In C, array subscripts begin at 0 and continue through sarray size 1.
- Therefore, the above array can be accessed using the following references: arrayOfTen[0], arrayOfTen[1]
arrayOfTen[9].
- Let us define parrayOfTen as a pointer to an integer as follows:
int *parrayOfTen;
- This implies that the following code sets parrayOfTen equal to the address of the first element in the array arrayOfTen.
parrayOfTen = &arrayOfTen[0];
- Now, the following assignment copies the contents of arrayOfTen[0] to x.
x = *parrayOfTen;
- Now, using what we learned earlier about pointers, the following points to the second element in arrayOfTen.
*(parrayOfTen + 1)
- The following code uses array subscripting to count the characters in a character string. It assumes charString is a character array containing a null terminated string.
int i, n=0;
for (i=0; charString[i] != \0; i++)
n++;
printf("The character string %s, contains %d characters", charString, n);
- The following code uses pointers to accomplish the same task.
int n=0;
for (;*charString != \0;charString++)
n++;
printf("The character string %s, contains %d characters", charString, n);
- There is a lot more to know about arrays and pointer. The best course to take, at this point, is to learn by doing. Complete the following exercises. Work through the tutorial referenced above. Peruse any standard C documentation and the UNIX man pages.
Exercises
- Code a swap function to swap the contents of two character strings.
- Write a function to concatenate two character strings.
- Write a function to copy the contents of one character string to another.