Pointers in C Programming(Concepts)

Concepts of Pointers

From our basic understanding of Pointers in C we learned that the value of a pointer is the address in memory of another variable. Not only is the pointer just the address location but it can also be used to retrieve the value of the variable that it points to. One of the other concepts that pointers can have is arithmetic. A pointer is the address to another variable but that address is a hexadecimal number, which is a numeric value. With pointers there are four different arithmetic operators that can be used: ++, — -, +, — . When using the operations, depending on which is used it will move the pointer to the next memory location according to the operator used. For example :

#include <stdio.h>const int MAX = 3;int main () {    int arr[] = {10, 20, 30};
int i, *pntr;
// We will have the array address location as the pointer
pntr = arr;
for (i = 0; i < MAX; i++) {
printf("Address of var[%d] = %x\n", i, pntr);
printf("Value of var[%d] = %d\n", i, *pntr);
// Increment our pointer address location
pntr++;
}
return 0;
}

With the above example we can see that we have an array and we define a pointer to have the value of the address location of that array. We then perform a simple counter and have it display the address location as well as the value of the new incremented variable. The above code when compiled and ran would look like this :

Address of var[0] = d5f3d0c4Value of var[0] = 10Address of var[1] = d5f3d0c8Value of var[1] = 20Address of var[2] = d5f3d0ccValue of var[2] = 30

The address location increments without changing the value of the pointed to variable. This is how the simple arithmetic concept with pointers works and you can also see how you can even store the memory location of an array in a pointer. Another concept with pointers is having an array of pointers.

You can have an array of pointers in C which means that each element within the pointer would have a pointer to its value. In this next example we will have an array of pointers, we will set the pointer as an array with MAX integer pointers.

#include <stdio.h>const int MAX = 3;int main () {

int arr[] = {10, 20, 30};
int i, *pntr[MAX];
for ( i = 0; i < MAX; i++) {
pntr[i] = &var[i];
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *pntr[i]);
}
return 0;
}

In the above example we have the same array as previous example but we se the pntr to have a max array of three. Then we set each individual element within the array to its own pointer by using pntr[i] = &var[i] . Each element within the array would have its own set pointer with its own memory address and value.

One other concept of pointers is that a pointer can point to another pointer. Might seem a little confusing but really it is the same concept as a regular pointer. A regular pointer has a value of a stored address location, when a pointer points to another pointer it would have the value of the address location of the pointer that it is pointing to. An example for this concept is the easiest way to explain it.

#include <stdio.h>int main () {

int targetVariable;
int *pointerToVariable;
int **pointerToPointer;
targetVariable = 3000; pointerToVariable = &targetVariable; pointerToPointer = &pointerToVariable; printf("Value of target variable = %d\n", targetVariable);
printf("Valua available at pointerToVariable = %d\n", *pointerToVariable);
printf("Value available at pointerToPointer = %d\n", **pointerToPointer);
return 0;
}

First off we need to declare the pointerToPointer as being a pointer that points to another pointer. That is why we declared it like **pointerToPointer an additional asterisk is needed for this type of pointer. Then we set the value of pointerToVariable to the address location of targetVariable by using pointerToVariable = &targetVariable then we set the value of pointerToPointer to the address location of pointerToVariable . We can access the value of targetVariable using both pointers the result of the above example would look like this :

Value of target variable = 3000Value available at pointerToVariable = 3000Value available at pointerToPointer = 3000

With this example you can see that even pointerToPointer can access the value of the pointed-to variable of another pointer. Pointers are a very useful and tricky concept in C but understanding how they work and what they do can be a huge help when building C programs that might require some pointers.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store