📍🔥Advanced
Declare 2D Array of Pointers
2D pointer array declaration
Declare and use a two-dimensional array of pointers - useful for storing strings of different lengths.
Program Code
2d_pointer_array.c
C
1#include <stdio.h>23int main() {4 // 2D array of pointers to strings5 char *names[5] = {6 "Alice",7 "Bob",8 "Charlie",9 "David",10 "Eve"11 };12 13 printf("Names using 2D pointer array:\n");14 for (int i = 0; i < 5; i++) {15 printf("names[%d] = \"%s\"\n", i, names[i]);16 }17 18 // 2D pointer array for integers19 int row1[] = {1, 2, 3};20 int row2[] = {4, 5, 6, 7};21 int row3[] = {8, 9};22 23 int *matrix[] = {row1, row2, row3};24 int sizes[] = {3, 4, 2};25 26 printf("\nJagged array (rows of different sizes):\n");27 for (int i = 0; i < 3; i++) {28 printf("Row %d: ", i);29 for (int j = 0; j < sizes[i]; j++) {30 printf("%d ", matrix[i][j]);31 }32 printf("\n");33 }34 35 return 0;36}Output
Names using 2D pointer array:
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Charlie"
names[3] = "David"
names[4] = "Eve"
Jagged array (rows of different sizes):
Row 0: 1 2 3
Row 1: 4 5 6 7
Row 2: 8 9
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials