A pointer is a variable that stores a memory address. A pointer declaration takes the following form:
datatype *pointer_name;
Here, datatype is the pointer's base type and must be a valid C data type and pointer_name is the user-defined name. The asterisk indicates that the declaration is a pointer. The base type is important because the compiler needs to know how many bytes make up the value pointed to.
Initialising a Pointer.
Pointer initialisation is the process of assigning the address of a variable to a pointer variable. Each pointer is set to the address of the first byte of the pointed-to variable. When a variable's address is unknown during the declaration, a null value should be assigned to the pointer. A pointer which is assigned a null value is called a null pointer. A non-initialised pointer is called a wild pointer and will contain a random or junk value. Wild pointers are dangerous because they cause a program to access invalid memory locations leading to unpredictable results. To store the address of this variable in a pointer use the referencing operator.
Assigning and Accessing Values
The indirection operator or dereferencing operator (*) operates on a pointer and returns the value stored in the address kept in the pointer variable.
In the example below, pTr is created as an int pointer initialised with a NULL value. This pointer is then assigned to the address of the int variable a_value . The dereferencing operator is then used to output the value stores at the address of pTr.
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int a_value = 30; // declares int variable to value 30 int * pTr=NULL;// declares pointer name pTr of type int and assigns NULL value pTr= &a_value; //sets pointer pTr to address of a_value printf("%d",*pTr); }
Pointer Arithmetic
There are only four arithmetic operators that can be used on pointers: ++, – –, +, and –
An increment or decrement operation on a pointer will point to the next value in the memory block. The new location will represent the location of the next variable value and not the next byte of memory. For instance, using the ++ on an int pointer tells the compiler to point to the next consecutive integer. Decrementing pointers ( -- ) has the opposite effect. The pointer address is decremented by the size of the type being pointed to. This ensures that the pointer points to the beginning of some valid data and not an arbitrary memory location. In the case of char pointer, an increment or decrement will only change the pointer value by 1 since characters are one byte long. Every other pointer type will increase or decrease by the size of its base type.
Pointer to an Array
Pointers can also be used to address elements of an array. When an array is declared the compiler allocates sufficient memory to access all the elements of that array. The base address is the address of the first element. If a pointer is declared to point to the array then each array element can be accessed by increasing or decreasing this pointer.
int main() { int a[5]={1,2,3,4,5};//declare array int *ptr=a;//set pointer *ptr to start of array for (int i=1;i<=5;i++) { printf ("%d",*ptr);//dereference value held at pointer address *ptr++;//increase pointer }
}
Note that there is no requirement to use the address operator (&) when assigning a pointer to an array.