C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we will learn about arrays of pointers in C programming language. An array of pointers is a data structure where each element is a pointer, which can store the memory address of another variable or object.
Creating an array of pointers:
To create an array of pointers, you should specify the data type of the pointers, followed by the *
symbol and the array name, along with the size of the array in square brackets.
For example, let's create an array of 5 integer pointers:
int *ptr_array[5];
Initializing an array of pointers:
You can initialize the elements of the array by assigning them the addresses of variables of the appropriate data type. Here's an example of initializing an array of integer pointers:
int num1 = 10, num2 = 20, num3 = 30, num4 = 40, num5 = 50; int *ptr_array[5] = {&num1, &num2, &num3, &num4, &num5};
Accessing the values using an array of pointers:
To access the values of the variables that the pointers in the array are pointing to, you can use the dereference operator *
. Here's an example of accessing the values using an array of integer pointers:
#include <stdio.h> int main() { int num1 = 10, num2 = 20, num3 = 30, num4 = 40, num5 = 50; int *ptr_array[5] = {&num1, &num2, &num3, &num4, &num5}; for (int i = 0; i < 5; i++) { printf("Value of num%d: %d\n", i+1, *ptr_array[i]); } return 0; }
Output:
Value of num1: 10 Value of num2: 20 Value of num3: 30 Value of num4: 40 Value of num5: 50
Using an array of pointers with strings:
Arrays of pointers are particularly useful when working with arrays of strings. In this case, each pointer can store the memory address of the first character of each string. Here's an example of using an array of character pointers to store and print an array of strings:
#include <stdio.h> int main() { const char *str_array[4] = {"Apple", "Banana", "Cherry", "Date"}; for (int i = 0; i < 4; i++) { printf("String %d: %s\n", i+1, str_array[i]); } return 0; }
Output:
String 1: Apple String 2: Banana String 3: Cherry String 4: Date
In summary, an array of pointers in C programming language is a data structure where each element is a pointer. This can be particularly useful for working with arrays of strings or for other scenarios where you need to store and manipulate the memory addresses of multiple objects. Understanding how to create, initialize, and access arrays of pointers will enable you to create more efficient and flexible programs in C.
Creating and Initializing Pointer Arrays in C Language:
Pointer arrays store the addresses of variables, allowing dynamic access.
#include <stdio.h> int main() { int arr[3] = {1, 2, 3}; int *ptrArr[3]; // Pointer array for (int i = 0; i < 3; ++i) { ptrArr[i] = &arr[i]; // Initializing pointer array printf("Element %d: %d\n", i + 1, *ptrArr[i]); } return 0; }
Output:
Element 1: 1 Element 2: 2 Element 3: 3
ptrArr
is an array of pointers pointing to elements of arr
.
Accessing and Modifying Elements of a Pointer Array:
Elements of a pointer array can be accessed and modified dynamically.
#include <stdio.h> int main() { int arr[3] = {1, 2, 3}; int *ptrArr[3]; for (int i = 0; i < 3; ++i) { ptrArr[i] = &arr[i]; } *ptrArr[1] = 42; // Modifying an element through the pointer array printf("Modified Element 2: %d\n", arr[1]); return 0; }
Output:
Modified Element 2: 42
Modifying through the pointer array affects the original array.
Arrays of Pointers vs. Pointers to Arrays in C Programming:
Arrays of pointers store multiple pointers, while pointers to arrays point to entire arrays.
#include <stdio.h> int main() { int arr1[] = {1, 2, 3}; int arr2[] = {4, 5, 6}; int *arrPtrs[2] = {arr1, arr2}; // Array of pointers for (int i = 0; i < 2; ++i) { printf("Element %d of array %d: %d\n", 2, i + 1, arrPtrs[i][2]); } return 0; }
Output:
Element 2 of array 1: 3 Element 2 of array 2: 6
arrPtrs
is an array of pointers to arrays.
C Code Examples Illustrating Pointer Array Usage:
#include <stdio.h> int main() { char *names[] = {"Alice", "Bob", "Charlie"}; // Array of string pointers for (int i = 0; i < 3; ++i) { printf("Name %d: %s\n", i + 1, names[i]); } return 0; }
Output:
Name 1: Alice Name 2: Bob Name 3: Charlie
Demonstrates an array of pointers to strings.
Dynamic Allocation of Pointer Arrays in C:
Dynamic memory allocation allows flexibility in size.
#include <stdio.h> #include <stdlib.h> int main() { int size = 4; int *dynamicArray = (int *)malloc(size * sizeof(int)); // Dynamic allocation if (dynamicArray != NULL) { for (int i = 0; i < size; ++i) { dynamicArray[i] = i + 1; printf("%d ", dynamicArray[i]); } free(dynamicArray); // Release allocated memory } else { printf("Memory allocation failed.\n"); } return 0; }
Output:
1 2 3 4
Demonstrates dynamic allocation of an array using pointers.
Array of Strings Using Pointer Arrays in C:
#include <stdio.h> int main() { char *colors[] = {"Red", "Green", "Blue"}; // Array of string pointers for (int i = 0; i < 3; ++i) { printf("Color %d: %s\n", i + 1, colors[i]); } return 0; }
Output:
Color 1: Red Color 2: Green Color 3: Blue
Illustrates an array of pointers to strings.
Pointer Array Arithmetic and Traversal in C:
Pointer arithmetic allows easy traversal through arrays.
#include <stdio.h> int main() { int numbers[] = {10, 20, 30, 40, 50}; int *ptr = numbers; // Pointer pointing to the first element for (int i = 0; i < 5; ++i) { printf("%d ", *(ptr + i)); // Pointer arithmetic } return 0; }
Output:
10 20 30 40 50
Demonstrates traversing an array using pointer arithmetic.
Multidimensional Arrays and Pointer Arrays in C Programming:
Multidimensional arrays can be represented using pointer arrays.
#include <stdio.h> int main() { int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; int *ptrArray[2]; for (int i = 0; i < 2; ++i) { ptrArray[i] = matrix[i]; for (int j = 0; j < 3; ++j) { printf("%d ", *(ptrArray[i] + j)); } printf("\n"); } return 0; }
Output:
1 2 3 4 5 6
ptrArray
is an array of pointers to arrays, representing a 2D array.