Skip to main content

Using Pointers in C

Uses of Pointers in C

In C, pointers are an important feature that allow you to work with memory addresses directly.

By using pointers, you can allocate and deallocate memory dynamically, manipulate data structures efficiently, and write more efficient code overall.

Here are some common use cases for pointers in C:

Dynamic memory allocation

  • C provides functions like malloc(), calloc(), and realloc() that allow you to allocate memory dynamically at runtime.
  • Pointers are used to store the address of the allocated memory block, so that you can access and manipulate the data stored in that block.

Passing arguments to functions

  • When you pass an argument by reference, you are actually passing a pointer to the data.
  • This allows the function to access and modify the original data, rather than a copy.

Working with arrays

  • By using pointer arithmetic, you can access and manipulate individual elements of the array, or iterate over the entire array.

Data structures

  • Pointers are essential for working with complex data structures like linked lists, trees, and graphs.
  • By using pointers to store references to other nodes or elements in the data structure, you can create dynamic, efficient data structures that can be modified and manipulated easily.

Interfacing with hardware

  • By using pointers to read and write directly to memory addresses, you can control hardware devices and perform low-level operations.

Examples

Example: Dynamic memory allocation

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *ptr;
int n = 5;

// Allocating memory dynamically using malloc()
ptr = (int *)malloc(n * sizeof(int));

// Checking if memory allocation was successful
if (ptr == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}

// Setting values for the array elements
for (int i = 0; i < n; i++)
{
ptr[i] = i * 10;
}

// Printing the values of the array elements
printf("The elements of the array are:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", ptr[i]);
}
printf("\n");

// Freeing the dynamically allocated memory using free()
free(ptr);

return 0;
}
Output:

Explanation

  • We declare a pointer variable ptr and a variable n to hold the size of the array we want to allocate.
  • We then use the malloc() function to allocate a block of memory of size n * sizeof(int). This will create an array of n integers in memory, and return a pointer to the first element of the array.
  • We use a loop to set the values of the array elements to multiples of 10.
  • We then use another loop to print the values of the array elements to the console.
  • Finally, we use the free() function to release the dynamically allocated memory back to the operating system
  • When we run the program, it will allocate an array of 5 integers and set their values to 0, 10, 20, 30, and 40.
  • It will then print the values of the array elements to the console.

Example: Working with arrays

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *ptr;
int n = 5;

// Allocating memory dynamically using malloc()
ptr = (int *)malloc(n * sizeof(int));

// Checking if memory allocation was successful
if (ptr == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}

// Setting values for the array elements
for (int i = 0; i < n; i++)
{
ptr[i] = i * 10;
}

// Printing the values of the array elements
printf("The elements of the array are:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", ptr[i]);
}
printf("\n");

// Freeing the dynamically allocated memory using free()
free(ptr);

return 0;
}
Output:

Explanation

  • We declare an integer array arr with 5 elements and initialize it with some values.
  • We then declare a pointer ptr and initialize it to the address of the first element of the array using the array name.
  • Note that when an array name is used in an expression, it evaluates to a pointer to the first element of the array.
  • We then use a loop to print the elements of the array using both array notation and pointer notation.
  • In the array notation, we use the square brackets to access the elements of the array.
  • In the pointer notation, we use the dereference operator (*) to access the value pointed to by the pointer, and add the loop counter i to the pointer to get to the next element of the array.