Exploring Array of Pointers in C – A Detailed Guide

Updated on September 13, 2024

Article Outline

In the realm of C programming, the concepts of Array and pointers are not just basic. They are crucial. Pointers, in particular, offer a significant advantage by enabling swift data access. This fusion of arrays and pointers allows for efficient manipulation of data within arrays. This concept also extends to multidimensional arrays and creating arrays of pointers to manage various kinds of data, making it a topic of utmost importance for any C programmer or student.

An Array of Pointers in C language

Arrays and Pointers in the C language have a very strong relationship. Pointers are variables that contain the addresses of some other variables, and with arrays, a pointer stores the starting address of the array. If the pointer variable stores the base address of an array, then we can manipulate all the array elements using the pointer variable only. We can also create an array of pointers to store multiple addresses of different variables.

*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

What is an Array?

An array is a data structure that allows you to store the data in the same type. These elements can be accessed using an index, which represents their position within the array. Arrays are commonly used for storing and manipulating elements in the C language.

 

Syntax of the Array

type arrayName[arraySize];

What is a Pointer?

A pointer variable stores the address of another variable. Pointers are powerful tools that allow direct memory access and manipulation.

 

The following program demonstrates the Pointer

int var = 10; int *p; p = &var;

What is an Array of Pointers in C language?

An array of Pointers. Pointer arrays commonly contain different pointer variables.  We can create individual pointer variables that point to various value ranges, or we can create a single integer array of pointers that point to all available values.

 

Syntax

 

In C language, array elements are denoted as arr[i], where i is the index value. Below is the similar syntax used in the pointer.

 

*(arr+i) ;

 

  • * asterisk for the dereferencing operator for extracting the value from the address
  • *(arr+i) is the same as arr[i] in a C program.
  • arr represents the array name i represents the index value.

 

The following program demonstrates the array of pointers in C language:

 

Program

#include <stdio.h>  int main() { int arr[5] = {234, 343, 6234, 343, 105}, i;  for(i = 0; i < 5; i++){ printf("[index %d] Address : %u, Value : %dn", i, (arr + i), *(arr + i)); } return 0; }

Output

[index 0] Address : 2485226688, Value : 234 [index 1] Address : 2485226692, Value : 343 [index 2] Address : 2485226696, Value : 6234 [index 3] Address : 2485226700, Value : 343 [index 4] Address : 2485226704, Value : 105

Array of Pointers to Character

We can also use an array of pointers for storing multiple strings as an array of pointers to characters. Here, each pointer in the array is a character pointer that points to the first character of the string in the array.

 

Syntax

char *array_name [array_size];

We can assign a string of any length to these pointers.

 

The following program demonstrates the Array of Pointers to Characters

 

Program

#include<stdio.h>  int main() { // Declaring an array of pointers to characters char *names[] = {"Neeraj", "Harry Potter", "Voldemort", "Computer", "voldemort" };  // Accessing and printing the second name printf("%sn", names[0]); printf("%sn", names[1]); printf("%sn", names[2]); printf("%sn",names[3]) ;  return 0; }

Output

Neeraj Harry Potter Voldemort Computer

In the above C program, we have declared five pointers. Each pointer in the C array points to the start of a string. The string “Neeraj” is pointed to by names[0] ,  and “Harry Pointer” by names [2]. By indexing into an array, we can access and manipulate individual strings very easily. 

 

The following program defined the array size before initializing the array of pointers:

 

Program

#include <stdio.h>  int main() {  char* arr[3] = { "Monday", "Tuesday", "Wednesday" };  for (int i = 0; i < 3; i++) { printf("%sn", arr[i]); }  return 0; }

Output

Monday Tuesday Wednesday

Memory Representation of an Arrays of Strings

 

  1. Arrays of Pointers: An array of pointers to hold strings means you’re essentially reserving space in memory for the pointers, not the actual strings. Each element of this array is a pointer. Typically of size 4 bytes for a 32-bit system or 8 bytes for a 64-bit system. Which stores the address of the locations where the actual strings begin.
  2. Actual Strings:  The strings themselves reside in different parts of memory locations. They initialize the array of pointers with string literals (like “Neeraj” or “John”) These literals are typically stored in a read-only section of memory.

 

The following image describes the memory representation of an Array of Strings.

memory representation

An Array of Pointers to Different Types

In C language, we can declare arrays of pointers with strings. The concept of arrays of pointers is not restricted to characters alone. We can array pointers to various data types, such as ints and float structures. The following program demonstrates the Array of Pointers:

 

Program

#include<stdio.h>  int main() { int a = 343; float b = 55.34; char c = 'N';  void *arr[3]; arr[0] = &a; arr[1] = &b; arr[2] = &c;  printf("Integer value: %dn", *((int *)arr[0])); printf("Float value: %fn", *((float *)arr[1])); printf("Char value: %cn", *((char *)arr[2]));  return 0; }

Output

Integer value: 343 Float value: 55.340000 Char value: N

Output

Integer value: 343 Float value: 55.340000 Char value: N

Application of Array of Pointers

In C language, an array of pointers is a powerful tool in programming and has various applications. Here are some common applications of Array of Pointers:

 

  • Dynamic Memory Allocation: Array of Pointer mostly used in the where we used dynamic memory allocation. Instead of creating a fixed array size. We can create a dynamic memory allocation for each element using pointers. This allows for flexible memory management.

 

  • Function Pointers: A function pointer variable stores the address of the function. An array of function pointers can be used to implement function tables or callback mechanisms, which are commonly used in event-driven programming.

 

  • Multidimensional Arrays: Arrays of pointers are also used for creating multidimensional arrays where each  ‘row’ is a dynamically allocated array of a different size.

 

  • Strings: In C programming, each character is stored in an array of characters, often referred to as a character array or C-String. An Array of Pointers can be used to store multiple strings, where each pointer points to the first character of each string. An array of Pointer allows memory efficiency, especially when dealing with large datasets.

Disadvantages of Array of Pointers

An Array of Pointers also has some disadvantages. Here, we are discussing some disadvantages of an Array of Pointers:

 

  • Higher Memory Consumption: An array of pointers consumes more memory because it offers dynamic storage adventures. The pointers themselves consume memory. For example, on a 64-bit system, every pointer will typically consume 8 bytes of memory.

 

  • Complexity: An array of pointers increases the complexity of the code. It makes, It more challenging to read and understand, especially for those new to the concept of pointers.

 

  • Less Readability: Arrays of the pointer may be less readable in c language code. Indirection through pointers can make it harder to understand the code’s logic and flow especially for developers. That is not familiar with an array of pointers.

Conclusion

In this article, we learned about Arrays of Pointers. Arrays are collections of variables of the same type that can be accessed by indexing. Pointers are variables that store the memory address of the other variables. An Array of Pointers is valuable for storing multiple addresses.  With a proper understanding of the Array of Pointers, the Developer can harness the full potential of arrays of pointers to build an efficient and robust C program.

FAQs
In the C language, an array of pointers is an array where each element is a pointer to a memory location. An array of pointers allows for dynamic memory allocations, flexible data structures, and efficient management of heterogeneous data types.
In C, we can declare an array of pointers by specifying the base type of the pointers followed by square brackets, which contain the array's size. For example, “int *array[5];” declares an array of 5-pointers to integers.
In C language, we can access elements of an array of pointers using array subscript notation. For example, “*ptrArray[i]” accesses the value stored at the memory locations pointed to by the ith element of ‘ptrArray'.
Yes, we can create multidimensional arrays where each “row” is a dynamically allocated array of different sizes. This allows for flexibility in handling irregular or jagged data structures.

Updated on September 13, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved