Home | API | MFC | C++ | C | Previous | Next

Programming With C

Pointers

A pointer is a variable that stores a memory address that points to another variable. A pointer declaration takes the following form:

datatype *pointer_name;

Here, a type is the pointer's base type and must be a valid C data type and pointer_name is the user-defined name for the pointer. The asterisk * is used to indicate that the declaration is a pointer. Since a non-generic pointer directly references another variable value it is necessary to specify which data type a pointer is going to point to. This is known as the base type. The base type is important because the compiler needs to know how many bytes make up the value pointed to.

Initialising a Pointer.

Until a pointer holds the address of a variable, it isn’t useful. Pointer Initialisation is the process of assigning the address of a variable to a pointer variable. Each pointer is equal to the address of the first byte of the pointed-to variable. In the event that a variable's address is not known during the declaration, it is recommended that a null value is assigned to the pointer variable. A pointer that is assigned a null value is called a null pointer. A pointer that is not initialised is called a wild pointer and it 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 when the pointer is used. 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 operators are then used to output the value stored at the address of pTr.

#include <stdlib.h>
int main()
{
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 block of memory. 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 is telling the compiler that you want it to point to the next consecutive integer. Decrementing pointers ( -- ) has the opposite effect. The address contained in the pointer is incremented or decremented by the sizeof the type being pointed to. This ensures that the pointer only points to the beginning of some valid data and not to some arbitrary memory location. In the case of character pointers, an increment or decrement will only change the pointer value by 1 since characters are one byte long. Every other type of pointer will increase or decrease by the length of its base type.

Pointer to an Array

In addition to storing the address of a single variable, 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 element of the array 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


Home | API | MFC | C++ | C | Previous | Next
The Basics | Variables | Constants | Expressions and Operators | Controlling Program Flow | Input & Output | C++ Functions | Arrays | Characters and Strings | String Manipulation | Pointers | Structures | Unions | File Input and Output | Advanced Pointers | Type Conversion | Dynamic Memory Allocation

Last Updates:25 October 2022