Pointers in C
When a variable is created, it is stored in memory and a specific address is assigned to it. Pointers are variables that store memory addresses of other variables. They allowing you to manipulate data indirectly by referring to the memory location where the data is stored. The data stored in pointers is in hexadecimal form.
First we will see how can we get the address of a variable with the help of "&" operator. We store the address in pointer variable.
Declaring Pointers
We declare pointers using * before the name of the pointer. It is declared like a simple variable but we put * before the name of the variable.
There are two ways to declare a pointer variable;
- int* ptr;
- int *ptr;
Dereferencing
To access the value pointed to by a pointer, you use the asterisk (*) symbol again.
Do pointers have data types?
Yes, pointers in C do have data types associated with them. Pointers are used to store memory addresses, and the data type of a pointer indicates the type of data that the pointer can point to.
- int *ptr means ptr is pointing to int type variable.
- float *ptr means ptr is pointing to float type data.
Note: If we do not want to specify the data type of pointer or if we do not know what kind of data pointer is pointing to, we can set the data type of pointer "void".
Pointers to pointers
Pointers can also point to other pointers. These are called "double pointers" or "pointers to pointers."
Pointers and Arrays
Pointers and arrays in C are closely related concepts. In fact, in C, arrays are implemented as a contiguous block of memory, and the array name acts as a pointer to the first element of the array.
In this example;
- Initializing an int type array of size 5.
- Initializing a pointer to array. marks atore the base address of array. It points to the first element of the array (90).
- Loop starts and ptr has address of array[0]. i = 0. So, it will point to 90.
- In the next iteration, i = 1. So it will print ptr + 1 which is 80.
- In the next iteration, i = 2. So it will print ptr + 2 which is 88.
- In the next iteration, i = 3. So it will print ptr + 3 which is 95.
- In the next iteration, i = 4. So it will print ptr + 4 which is 78.
- In the next iteration, i = 5. condition becomes false. The controll moves out of the loop.
Importance of Pointers
- Pointers allow you to allocate memory dynamically at runtime, which is essential for tasks like creating data structures (e.g., linked lists, trees, and dynamic arrays) and working with variable-sized data.
- C is often used to build system-level and embedded applications that interact closely with the operating system. Pointers enable interaction with system calls and kernel-level programming.
- Unlike Java and some C++ libraries, C does not have automatic garbage collection.
- Pointers in C can be used to directly access memory addresses, making it possible to interface with hardware, memory-mapped I/O, and perform low-level system programming.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP