The pointer is just another data type in C or C++. Just like how an integer( a 4-byte ones) is used to store numbers, pointers is a data type that allows storing addresses.
When you run your programs, each program runs under a process and each process is given some memory by the system. All the data like variables, values, etc., are stored in this memory. This memory resides in RAM. Each byte of RAM has its unique identifier called as it’s address. Pointers in C allows us to access those addresses directly and manipulate them as well.
printf("%d",i); //print value stored in memory occupied by i
printf("%d",&i); //print memory address of variable i
The line printf(“%d”,i) use the format specifier %d to print integers. In the next line %p is used to print addresses or pointer types in C. The &i uses the address of the operator. It means that & is used to get the address of i. The %p format specifier and the & operator are necessary while working with pointers.
STORING ADDRESSES AND POINTERS
A pointer as a variable can store addresses just like how your int variables can store positive and negative numbers and your float variables can store positive and negative real numbers. C uses * operator for the creation of pointers. The * operator has multiple usages other than multiplication. The syntax of creating a pointer in Python looks like this,
datatype* variable name;
The datatype can be of any kind of datatype that is supported by C like int, char, double, etc. We will even see pointers to structures as well. C is a strictly typed language. So a pointer to an integer canpoint only to integers or addresses where integers are stored.
See the code snippet below,
iptr is a variable that can store address of integer variables and fptr is a
int * iptr;
float * fptr;
variable that can store address of float variables. And both iptr and fptr are pointers. Now we can write code that can store the address of a variable just like below.
int * iptr = &i; //store address of i in iptr
printf("%p",i); //print value stored in memory occupied by i
printf("%p",&i); //print memory address of variable i
The variable iptr in the previous code is a variable that can point to any kind of integer data. It can store the address of any integer variable. Pointers are variables. They occupy some memory and also have an address.
ACCESSING POINTER DATA
The next job after creating a pointer is to access the data. We use the same * operator to do that. The different usage of & and * are mentioned below.
The previous code prints the value of the variable iusing the pointer iptr.
printf("%d\n",i); //print value stored in memory occupied by i
printf("%d\n", *iptr); //print data pointed to by iptr
printf("%d\n", *(&i)); //get the address of i and then dereference again
This step of getting data from the pointer is called dereferencing.
DYNAMIC MEMORY ALLOCATION AND POINTERS
The main purpose of pointers is to handle dynamic memory allocation and allocation of memory on the heap instead of pointing to stack memory. Heap memory can be considered as a free store of memory from where your program can request additional memory at runtime when needed and can return it back when not required. C provides functions like Malloc and are free to handle dynamic memory management. The only way to store and retrieve data from such memory locations is by address and pointers come to rescue.
© 2020 Apponix, All Rights Reserved.