You are on page 1of 6

C Programming B

Lecture Notes

C Pointers.
Definition

Any variable is stored in a certain memory location which is called "address". Pointers are used to access these locations and manipulate addresses rather than values of variables.

Addresses are also sets of values that can be manipulated. Pointer variables are assigned to the addresses.
& is a unary 'address' operator.

The address of a variable is often referred to as the left value. The right value refers to the content stored in the memory location of a variable.
Declaration and Initialisation

int *ptr; declares ptr to be of type pointer to integer variable . int *ptr = &i; declares ptr as a pointer to integer and initializes it to the address of the variable i.

For example, in the diagram above the integer value i is stored at memory location 600000, and the pointer to this variable ptr is stored at location 500000. The opposite operator, which gives the value at the end of the pointer is *. The dereference operator is a unary operator, whose operand is the address (left value) of a variable. An example of dereferencing pointer ptr would be
Dereferencing

O.Gredeskoul

AES RMIT 2002

C Programming B

Lecture Notes

i = *ptr; Do not confuse the many uses of the * sign: Multiplication, pointer declaration and pointer dereferencing. NOTE that the address of i and the value of pointer ptr are identical. The same applies to the value of i and to the dereferencing ptr: &i is the same as value ptr i is the same as *ptr The following program illustrates these facts.
Example 1 /* Deitel&Deitel ch 7 Using the & and * operators */ #include <stdio.h> #include <stdlib.h> int main(){ int a; int *aPtr;

/* a is an integer */ /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */ printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr ); printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr ); printf( "\n\nProving that * and & are inverses of " "each other.\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); system("PAUSE"); return 0; }

Format specifier % p in the printf() function displays the memory location as a hexadecimal integer. When you run this program, you should expect that the value of the pointer will be different on different machines.

O.Gredeskoul

AES RMIT 2002

C Programming B

Lecture Notes

Output: The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 Proving that * and & are complements of each other. &*aPtr = 0012FF88 *&aPtr = 0012FF88
Passing parameters by reference

You know that if the value of a variable is passed to a function it cannot be modifed by this function. If you wish to use arguments to modify the value of variables from a function, these arguments must be passed as pointers, and dereferenced within the function. This is called passing parameters by reference. Consider familiar swap function. It exchange values of two variables that are passed by reference.
void swap(int *p, int *q){ int temp; temp = *p; *p = *q; *q = temp; } /* temp has value that is at the address p */ /* value the address p now is the same as /* value at the address q */ /* value at the address q is assigned value of temp */

The following program can be used to test this function:


#include <stdio.h> #include <stdlib.h> void swap(int *p, int *q); int main() int int int { i = 5; j = 8; *iPtr, *jPtr;

iPtr = &i; jPtr = &j; printf("The original values are:"); printf("i = %d\tj = %d\n", i, j); swap(iPtr, jPtr); printf("The new values are:");

O.Gredeskoul

AES RMIT 2002

C Programming B

Lecture Notes

printf("i = %d\tj = %d\n", i, j); system("PAUSE"); return 0; } /* the swap function definition */

Guess, what is printed?


Arrays and Pointers

Pointers and arrays are related. An array's name is actually a pointer to the 0th element of the array. Dereferencing the array name will return the value of its 0th element. Because elements of an array are stored at the consecutive memory locations, we can access them using pointers. In the following examples, arr is an array.

Similarly, adding a number to the name of an array, we can access addresses of the array elements: Pointer arr arr + 2 arr + n
Pointer Arithmetic

Address &arr[0] &arr[2] &arr[n]

A function, which takes an array as a parameter, can declare that parameter in one of two ways. int arr[]; OR int *arr; The function sumArray() computes the sum of n elements of an array of double numbers. Two parameters of this function are the pointer to the double variable which is the first value in the sum, and the number of elements to be included in this sum.

O.Gredeskoul

AES RMIT 2002

C Programming B

Lecture Notes

double sumArray(double *arr, int n) {


int i; double sum = 0.0; for(i = 0; i < n; i++) sum += *(arr + i); return sum; }

If the first parameter is the name of an array, and the second is the array size, all elements of this array will be added to the sum. Consider an array of 100 elements that have been declared and assigned values in the main(). The following illustrates the use of pointer arithmetic. Various ways that sumArray() might be called Invocation
sumArray(num, 100) sum(num, 88) sum(&num[7], size - 7) sum(num + 7, 10)

What gets computed and returned


num[0] + num [1] +...+ num[99] num[0] + num[1] +...+ num[87] num[7] + num[8] +...+ num[size-1] num[7] + num[8] +...+ num[16]

Thus, the function call on line 1 computes the sum of all elements of the array num[]. The function call on line 2 computes the sum of the first 88 elements. The function call on line 3 returns the sum of elements from the 8 th (index 7) to the last (total (size-7) elements). The last line returns the sum of 10 elements of the array from 7 th to 16th.

Null Pointer A pointer is said to be a null pointer when its right value is 0. A null pointer can never point to valid data A null pointer can be used in control-flow statements or in a function as a return value. As any other varia ble, pointers can be initialised to zero, and can be assigned a value during the program execution. There might be more than one pointer assigned to the same address.

O.Gredeskoul

AES RMIT 2002

C Programming B

Lecture Notes

Pointer of the type void A pointer can be assigned to another pointer (or the address of a variable) if both pointers are of the same type. Otherwise, a cast operator must be used to convert the pointer on the right of the assignment to the pointer type on the left of the assignment. However if a pointer is declared as type void, the cast operator is not required. A pointer to void (void *ptr) is a generic pointer that can represent any pointer type. A pointer to void cannot be dereferenced. To retrieve a value at the memory location the compiler must know the data type to determine the number of bytes to be dereferenced.

O.Gredeskoul

AES RMIT 2002

You might also like