Pointers are widely used in programming; they are used to refer to memory location of another variable without using variable identifier itself. They are mainly used in linked lists and call by reference functions.
Diagram 1 illustrates the idea of pointers. As you can see below; Yptr is pointing to memory address 100.
Diagram 1: 1. Pointer and memory relationship
Declaring pointers can be very confusing and difficult at times (working with structures and pointer to pointers). To declare pointer variable we need to use * operator (indirection/dereferencing operator) before the variable identifier and after data type. Pointer can only point to variable of the same data type.
Datatype * identifier;
Figure 1 – Code Result
Description of code: In line 1 we are declaring char variable called a; it is initialized to character ‘b’, in line 2, the pointer variable ptr is declared. In line 4, the address of variable a is assigned to variable ptr. In line 6 value stored at the memory address that ptr points to is changed to ‘d’
Note: & notation means address-of operand in this case &a means ‘address-of a’.
Address operator (&) is used to get the address of the operand. For example if variable x is stored at location 100 of memory; &x will return 100.
This operator is used to assign value to the pointer variable. It is important to remember that you MUST NOT use this operator for arrays, no matter what data type the array is holding. This is because array identifier (name) is pointer variable itself. When we call for ArrayA; ‘ArrayA’ returns the address of first item in that array so ArrayA is the same as saying ArrayA+=2; and will return the third item in that array.
Pointers can be added and subtracted. However pointer arithmetic is quite meaningless unless performed on arrays. Addition and subtraction are mainly for moving forward and backward in an array.
Note: you have to be very careful NOT to exceed the array elements when you use arithmetic; otherwise you will get horrible errors such as “access violation”. This error is caused because your code is trying to access a memory location which is registered to another program.
Goes to the next memory location that the pointer is pointing to.
Goes to the previous memory location that the pointer is pointing to.
-= or –
Subtracts value from pointer.
+= or +
Adding to the pointer
Array and pointer arithmetic
Figure 2 Code 2 result
Description of ‘code 2’: in line 1 we are declaring ‘ArrayA’ integer array
variable initialized to numbers ‘1,2,3’, in line 2, the pointer variable ptr is
declared. In line 3, the address of variable ‘ArrayA’ is assigned to variable
ptr. In line 5 ptr is incremented by 1.
Note: & notation should not be used with arrays because array’s identifier is pointer to the first element of the array.
Pointers and functions
Pointers can be used with functions. The main use of pointers is ‘call by reference’ functions. Call by reference function is a type of function that has pointer/s (reference) as parameters to that function. All calculation of that function will be directly performed on referred variables.
Figure 3 code 3 result
Description of ‘code 3’: in line 1 we are declaring ‘DoubleIt’ function, in line 4, the variable number is declared and initialized to 2. In line 5, the function DoubleIt is called.
Pointer to Arrays
Array identifier is a pointer itself. Therefore & notation shouldn’t be used with arrays. The example of this can be found at code 3. When working with arrays and pointers always remember the following:
- Never use & for pointer variable pointing to an array.
- When passing array to function you don’t need * for your declaration.
- Be VERY CAREFUL not to exceed number of elements your array holds when using pointer arithmetic to avoid errors.
Pointers and Structures
Pointers and structures is broad topic and it can be very complex to include it all in one single tutorial. However pointers and structures are great combinations; linked lists, stacks, queues and etc are all developed using pointers and structures in advanced systems.
Figure 4. code 4 result
Description of ‘code 4’: in line 1-3 we are declaring ‘details’ structure, in line 4, the variable Maindetails is declared.in line 6, pointer is set to point to MainDetails. In line 7, 20 is assigned to MainDetails.num through structptr->num.
Pointer to Pointer
Pointers can point to other pointers; there is no limit whatsoever on how many ‘pointer to pointer’ links you can have in your program. It is entirely up to you and your programming skills to decide how far you can go before you get confused with the links. Here we will only look at simple pointer to pointer link. Pointing to pointer can be done exactly in the same way as normal pointer. Diagram below can help you understand pointer to pointer relationship.
Diagram 2. Simple pointer to pointer relationship
Code for diagram 2:
ptrA gives 100, *ptrA gives 101 , ptrb is 101 ,*ptrb gives b, **ptrA gives b
comment: **ptrA means ‘value stored at memory location of value stored in memory location stored at PtrA’