Applications of Pointers in C
Applications of Pointers in C
Applications of Pointers in C
One of the most important features of C is that it provides low-level memory access
with the concept of pointers. A pointer is a variable that stores the address of
another variable in the memory.
The provision of pointers has many applications such as passing arrays and struct
type to a function and dynamic memory allocation, etc. In this chapter, we will
explain some important applications of pointers in C.
The pointer to an array is the address of its 0th element. When the array pointer is
incremented by 1, it points to the next element in the array.
Example
The following example demonstrates how you can traverse an array with the help of
its pointer.
#include <stdio.h>
int main(){
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 1/8
6/16/24, 12:50 PM Applications of Pointers in C
Output
arr[0]: 1
arr[1]: 2
arr[2]: 3
arr[3]: 4
arr[4]: 5
malloc() function
Allocates an array of num elements each of which size in bytes will be size.
calloc() function
Allocates an array of num bytes and leaves them uninitialized.
realloc() function
Reallocates memory extending it up to newsize.
This function is defined in the "stdlib.h" header file. It allocates a block memory of
the required size and returns a void pointer.
The size parameter refers to the block of memory in bytes. To allocate the memory
required for a specified data type, you need to use the typecasting operator. For
example, the following snippet allocates the memory required to store an int type.
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 2/8
6/16/24, 12:50 PM Applications of Pointers in C
int *ptr;
ptr = (int * ) malloc (sizeof (int));
Here we need to define a pointer to character without defining how much memory is
required and later, based on requirement, we can allocate memory.
Example
In this example, we use the malloc() function to allocate the required memory to
store a string (instead of declaring a char array of a fixed size) −
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *name;
name = (char *) malloc(strlen("TutorialsPoint"));
strcpy(name, "TutorialsPoint");
if(name == NULL) {
fprintf(stderr, "Error - unable to allocate required memory\n");
} else {
printf("Name = %s\n", name );
}
}
Output
When the above code is compiled and executed, it produces the following output −
Name = TutorialsPoint
The C library function "calloc" (stands for contiguous allocation) allocates the
requested memory and returns a pointer to it.
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 3/8
6/16/24, 12:50 PM Applications of Pointers in C
Where "n" is the number of elements to be allocated and "size" is the byte size of
each element.
The following snippet allocates the memory required to store 10 integer types.
int *ptr;
ptr = (int *) calloc(25, sizeof(int));
The first parameter "ptr" is the pointer to a memory block previously allocated with
malloc, calloc or realloc to be reallocated.
First, it overcomes the limitation of pass by value. Changes to the value inside the
called function are done directly at the address stored in the pointer. Hence, we can
manipulate the variables in one scope from another.
Second, it also overcomes the limitation of a function in that it can return only one
expression. By passing pointers, the effect of processing a function takes place
directly at the address. Secondly, more than one value can be returned if we return
the pointer of an array or struct variable.
Example
The following function receives the reference of two variables whose values are to be
swapped.
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 4/8
6/16/24, 12:50 PM Applications of Pointers in C
return 0;
}
Example
The main() function has two variables "a" and "b", their addresses are passed as
arguments to the swap() function.
#include <stdio.h>
int main(){
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 5/8
6/16/24, 12:50 PM Applications of Pointers in C
In this program, we have been able to swap the values of two variables out of the
scope of a function to which they have been passed, and we could overcome the
limitation of the function’s ability to pas only one expression.
The max() function traverses the array using the pointer and returns the largest
number in the array, back to the main() function.
Example
#include <stdio.h>
int main(){
if ((*arr)>max)
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 6/8
6/16/24, 12:50 PM Applications of Pointers in C
max = (*arr);
arr++;
}
return max;
}
Output
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
max: 78
The max() function receives the address of the array from the main() function in the
pointer "arr". Each time, when it is incremented, it points to the next element in the
original array.
Example
The following example demonstrates how you can return multiple values with the
help of C pointers.
#include <stdio.h>
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 7/8
6/16/24, 12:50 PM Applications of Pointers in C
int main() {
int num1 = 10;
int num2 = 3;
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_applications_of_pointers.htm 8/8