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 how to use pointers with two-dimensional arrays in the C programming language. A two-dimensional array is an array of arrays, and a pointer to a two-dimensional array is a pointer that points to the first element (the first one-dimensional array) of the 2D array.
To declare a pointer to a two-dimensional array, specify the data type, followed by an asterisk (*) for the pointer, the name of the pointer, and the dimensions enclosed in square brackets, with the first dimension omitted.
int (*arrayPointer)[4];
In this example, arrayPointer
is a pointer to a one-dimensional array of 4 integers. It can be used to point to a two-dimensional array with any number of rows but 4 columns.
To assign the address of a two-dimensional array to a pointer, use the name of the array (which is a pointer to the first element).
int myArray[3][4]; arrayPointer = myArray;
To access elements in a two-dimensional array using a pointer, use the pointer name followed by the row and column indices enclosed in square brackets.
int value = arrayPointer[1][2]; // Access the element in the second row and the third column
Here's an example demonstrating how to iterate over a two-dimensional array using a pointer:
#include <stdio.h> int main() { int myArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; int (*arrayPointer)[4] = myArray; // Iterate over the 2D array using a pointer for (int row = 0; row < 3; ++row) { for (int col = 0; col < 4; ++col) { printf("%d ", arrayPointer[row][col]); } printf("\n"); } return 0; }
The output will be:
1 2 3 4 5 6 7 8 9 10 11 12
That's it for our tutorial on pointers to two-dimensional arrays in the C programming language. Understanding how to use pointers with 2D arrays is essential when working with dynamic memory allocation or when passing 2D arrays to functions.
Pointer to 2D array in C example:
#include <stdio.h> int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Pointer to 2D array int (*ptr)[4] = twoDArray; // Access elements using pointers printf("Value at (1,2): %d\n", ptr[1][2]); return 0; }
How to declare and use 2D array pointers in C:
#include <stdio.h> int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Declare and initialize a pointer to a 2D array int (*ptr)[4] = twoDArray; // Access elements using pointers printf("Value at (1,2): %d\n", ptr[1][2]); return 0; }
Accessing elements with 2D array pointers in C:
#include <stdio.h> int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Pointer to 2D array int (*ptr)[4] = twoDArray; // Access elements using pointers printf("Value at (1,2): %d\n", ptr[1][2]); return 0; }
Passing 2D array pointers to functions in C:
#include <stdio.h> // Function taking a pointer to a 2D array void printArray(int (*arr)[4]) { for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { printf("%d ", arr[i][j]); } printf("\n"); } } int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Pass 2D array pointer to function printArray(twoDArray); return 0; }
Dynamic allocation of 2D array using pointers in C:
#include <stdio.h> #include <stdlib.h> int main() { int rows = 3, columns = 4; // Dynamic allocation of a 2D array int (*dynamicTwoDArray)[columns] = (int (*)[columns])malloc(rows * sizeof(int[columns])); // Access and assign values dynamicTwoDArray[1][2] = 5; // Free memory free(dynamicTwoDArray); return 0; }
Pointer arithmetic with 2D arrays in C:
#include <stdio.h> int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Pointer arithmetic to access elements int* ptr = &twoDArray[0][0]; printf("Value at (1,2): %d\n", *(ptr + 1 * 4 + 2)); return 0; }
C pointer to array of pointers (2D array):
#include <stdio.h> int main() { int row1[] = {1, 2, 3, 4}; int row2[] = {5, 6, 7, 8}; int row3[] = {9, 10, 11, 12}; // Pointer to an array of pointers int* ptrArray[] = {row1, row2, row3}; // Access elements using pointers printf("Value at (1,2): %d\n", ptrArray[1][2]); return 0; }