Array of Pointers in C
“In C, an array of pointers is an array where each element is a memory address pointing to another variable or data structure. It is used to manage and manipulate data dynamically.”
A group of pointers to variables of a specific data type makes up an array of information. Each element of the array points to a different memory location, and the variety remains in memory as a single continuous block.
In C, a pointer array is a set of elements, each pointing to a different variable or data structure through a memory address. They are utilized when collections of pointers to additional data or dynamic memory allocation are required. Additionally, they are used to implement intricate data structures or multidimensional arrays with different size dimensions. In C, arrays of pointers offer a flexible method for handling and modifying data.
In C, an array is a group of related data types kept close to one another in memory. An index, an integer value that denotes the offset from the array's initial memory location, can be used to access the items of a collection.
A group of pointers to other variables or data structures makes up a C array of information. The array's elements are all pointers to various locations in memory. We can use an index to access the array's elements like a conventional array. However, we can utilize the tips to obtain the values indirectly rather than directly.
SYNTAX:
data_type *array_name[size];
pointer_type: a data type that the pointer is pointing to is indicated by pointer_type.
array_name: The array's reference array's name.
array_size the length of the pointers array.
To manage several variables effectively, we can utilize an array of pointers. Without having to make copies of the data, we can access and modify the values of the variables directly in memory by using arrows.
Note:
The operator for priority and associativity in the declarations of various pointers of various types must be kept in mind because even a tiny modification might have a significant impact. Putting *array_name in parenthesis, for instance, indicates that the array's name is a reference to a collection.
Creating a Pointer Array from Begin:
The ampersand (&) and the dereference operator (*) can be used together to assign the address of a variable to an entry of an array of pointers in C. For instance, you would write the following to initialize the first element of ptr_arr to point to an integer variable named b
int b = 12;
ptrArr[0]= &b;
Here, the array ptrArray's first element is given the address of the integer variable b.
Each pointer in the array has memory allocated for an integer variable using the malloc function. The sizeof operator is used to determine each integer variable's size. A void pointer that the malloc method returns is converted to an integer pointer and applied to the appropriate array element.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr[5]; // Create a declaration for a pointer array
int i, j;
for (i = 0; i < 5; i++) {
ptr[i] = NULL; // Set the array's pointers to null
}
for (i = 0; i < 5; i++) {
ptr[i] = (int *)malloc(sizeof(int)); // Make RAM available for each pointer
}
for (i = 0; i < 5; i++) {
*ptr[i] = i;
printf("%d\n", *ptr[i]); // Utilise the pointer array
}
for (j = 0; j < 5; j++) {
free(ptr[i]); // Release the memory used by the integer variables that were
allocated dynamically
}
return 0;
}
This program produces an array of 5 integer pointers, initializes each pointer to zero, allocates memory for each pointer, assigns the value of each dynamically generated integer variable to the loop index "j," and then prints the value using the printf function. The memory that was previously allocated using the malloc function is then deallocated.
Output:
A pointer array in C is an organized grouping of index pointer variables that reference specific memory locations. We typically use it to point at numerous memory locations with the same data type in our C program. Dereferencing the pointer that points to the data allows us to access it.
Example:
#include <stdio.h>
main()
{
int x = 10;
int y = 20;
int z= 30;
int *ptr_arr[3];
ptr_arr[0] = &x;
ptr_arr[1] = &y;
ptr_arr[2] = &z;
printf("The Values of x, y, z are %d, %d, %d\n", x, y, z);
printf("The values of the array are %d, %d, %d\n",
*ptr_arr[0], *ptr_arr[1], *ptr_arr[2]);
}
Output:
Explanation:
Three integer variables named x, y, and z are declared and given starting values of 10, 20, and 30, respectively. These variables have addresses that are assigned. This shows that the integer variables that the elements of the array of pointers are pointing to are accurate and that the dereference operator (*) can be used to access and output the values of those variables.
The array of String Pointers in C:
In the C programming language, a data structure called an array of pointers to strings is used to store and manage many lines. Each pointer in this array of pointers links to a different string in this data structure.
Let's look at how to declare the array of string pointers.
To declare the array of string pointers:
char *emp[5] = {
"Anil",
"Babu",
"Chary",
"Deeraj"
};
The code above declared an array of pointer names as 'emp' of size 5. Since initialization was completed in the example above at the time of declaration, the array size of a pointer does not need to be stated. You can rewrite the code above as follows:
char *emp[ ] = {
"Anil",
"Babu",
"Chary",
"Deeraj"
};
Each string literal in the 'emp' array in the scenario above would contain the base address of the first character of a string. For instance, emp[0] holds the base address of "Anil," emp[1] contains the base address of "Babu," and so on. The characters of a string literal are saved in a contiguous memory area, albeit it is not guaranteed that all string literals will be placed there.
Example:
#include <stdio.h>
int main() {
char *emp[] = {
"Anil",
"Babu,"
"Chary,"
"Deeraj"
};
int j;
for (j = 0; j < 4; j++) {
printf("%s\n", emp[j]);
}
return 0;
}
In this example, we declare the array "emp" as a pointer to a string. The array's elements are all pointers that each point to string literals. Four-string literals, "Anil," "Babu," "Chary," and "Deeraj," are used to initialize the array.
Then, we print each string in the array using a "for" loop. The string pointed to by the current pointer is printed using the printf function inside the loop.
Output:
One benefit of employing an array of pointers to strings is the ability to edit strings in C quickly. For instance, you can search for a particular string within the collection or sort the array according to the length of the strings. The pointers can point to strings of various sizes, enabling you to work with variable-length strings.
An array of pointers to a string and a two-dimensional array of characters different in the following ways:
- Memory allocator: Memory is allocated individually for each string in an array of pointers to strings. Each element of the array contains a pointer to a string kept elsewhere in memory.
- Flexibility: Because you may work with variable-length strings, an array of pointers to strings is more adaptable than a two-dimensional array of characters. You can quickly add or remove strings from the array without resizing the entire array because each pointer can point to a string of any length. Contrarily, a two-dimensional model of characters has a set number of characters, and you must be aware of the strings' maximum length in advance.
- Accessing specific strings: If you have an array of pointers to strings, you can quickly access particular strings by first getting the pointer to the string using the array index, then dereferencing the pointer to get the actual string. You must utilize a loop to access specific characters within each series in a two-dimensional array of characters.
- Sorting and searching: Strings are made more straightforward when using an array of pointers to strings since you may sort or search the pointers based on the strings they point to. You must sort or search within each string in a two-dimensional array of characters.
- Memory usage: Since each string needs its allocated block of memory, an array of pointers to strings may use more than a two-dimensional array of characters. In comparison, all the strings are kept in one memory block for a two-dimensional array of characters.
Advantages of Array Pointers in C:
- Dynamic memory allocation: This is possible with arrays of pointers, which is advantageous when the size of the data to be stored is variable or unknown.
- Flexibility: Because arrays of pointers can point to any data type, including arrays, structures, and other pointers, they are exceedingly adaptable.
- Memory efficiency: When working with massive data sets, arrays of pointers might save memory by merely maintaining memory addresses instead of the actual data.
- Multidimensional arrays: Using arrays of pointers to build multidimensional arrays with variable sizes for each dimension makes more adaptable and dynamic arrays possible.
- Function pointers can be stored in arrays of pointers, making writing more dynamic and adaptable code possible.
Disadvantages of Array Pointers in C:
- Complexity: Because working with arrays of pointers requires mastering both pointer and array syntax, they can be more challenging to use than conventional arrays.
- Memory management: Arrays containing pointers need more careful memory management than conventional arrays since, if not maintained correctly; they can result in memory leaks.
- Performance: Using arrays of pointers occasionally leads to slower performance than conventional displays since accessing data via pointers might be more time-consuming than accessing it directly.
- Indirection: Pointers can introduce additional indirection, making code more challenging to read and comprehend.