Applications of Pointers in C
Last Updated :
06 Jun, 2024
Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C.
Prerequisite: Pointers in C.
C Pointers Application
The following are some major applications of pointers in the C programming language:
1. Passing Arguments by Reference
Passing arguments by reference serves two purposes:
i.) to modify the variable in another function.
Example
The below example demonstrates the use of pointers by swapping two numbers.
C
// C program to demonstrate that we can change
// local values of one function in another using pointers.
#include <stdio.h>
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int x = 10, y = 20;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
ii.) For Efficiency Purpose
Example
The below example demonstrates the use of pointers to write efficient code.
C
#include <stdio.h>
// function to print an array by passing reference to array
void printArray(int* arr, int n)
{
// here array elements are passed by reference
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
printArray(arr, 5);
return 0;
}
Note: Passing large structure without reference would create a copy of the structure (hence wastage of space).
2. For Accessing Array Elements
Compiler internally uses pointers to access array elements. We can also use these pointers to access and modify the elements of the given array.
Example
The below example demonstrate the use of pointers to access elements of an array.
C
// C program to demonstrate that compiler
// internally uses pointer arithmetic to access
// array elements.
#include <stdio.h>
int main()
{
int arr[] = { 100, 200, 300, 400 };
// Compiler converts below to *(arr + 2).
printf("%d ", arr[2]);
// So below also works.
printf("%d\n", *(arr + 2));
return 0;
}
3. To Return Multiple Values
The functions in C can only return a single value, but we can use pointers to return multiple values from a C function.
Example
The below example demonstrates the use of pointers to return square and square root of numbers using pointers.
C
// C program to demonstrate that using a pointer
// we can return multiple values.
#include <math.h>
#include <stdio.h>
void fun(int n, int* square, double* sq_root)
{
*square = n * n;
*sq_root = sqrt(n);
}
int main()
{
int n = 100;
int sq;
double sq_root;
fun(n, &sq, &sq_root);
printf("%d %f\n", sq, sq_root);
return 0;
}
Output:
10000
10
4. For dynamic memory Allocation
We can use pointers to dynamically allocate memory i.e Dynamic memory allocation. The advantage of dynamically allocated memory is, it is not deleted until we explicitly delete it.
Example
The below example shows the use of pointers to dynamically allocate memory.
C
// C program to dynamically allocate an
// array of given size.
#include <stdio.h>
#include <stdlib.h>
int* createArr(int n)
{
int* arr = (int*)(malloc(n * sizeof(int)));
return arr;
}
int main()
{
int* pt = createArr(10);
return 0;
}
Similar Reads
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
9 min read
Pointer Arithmetics in C with Examples Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
Applications of Pointers in C Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
4 min read
Passing Pointers to Functions in C Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
2 min read
C - Pointer to Pointer (Double Pointer) In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
5 min read
Chain of Pointers in C with Examples Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
5 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
How to Declare a Pointer to a Function? A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
2 min read
Pointer to an Array | Array Pointer A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
5 min read
Difference between constant pointer, pointers to constant, and constant pointers to constants In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
3 min read