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 two-dimensional arrays in the C programming language. A two-dimensional array is essentially an array of arrays, where each element in the main array is another one-dimensional array. Two-dimensional arrays are useful when dealing with tabular data, such as a grid or a matrix.
To declare a two-dimensional array, specify the data type, followed by the name of the array and the dimensions enclosed in square brackets. The dimensions indicate the number of rows and columns in the array.
// Declare a 2D array with 3 rows and 4 columns int myArray[3][4];
You can initialize a two-dimensional array when declaring it by providing values enclosed in braces.
// Declare and initialize a 2D array int myArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
If you don't specify the number of rows, the compiler will automatically determine it based on the provided values.
int myArray[][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
To access elements in a two-dimensional array, use the array name followed by the row and column indices enclosed in square brackets.
int value = myArray[1][2]; // Access the element in the second row and the third column
You can use nested loops to iterate over a two-dimensional array. The outer loop iterates over the rows, while the inner loop iterates over the columns.
#include <stdio.h> int main() { int myArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Iterate over the 2D array for (int row = 0; row < 3; ++row) { for (int col = 0; col < 4; ++col) { printf("%d ", myArray[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 C two-dimensional array tutorial. Two-dimensional arrays are useful when dealing with matrix or grid-like data structures, and they can be easily manipulated using nested loops.
C programming 2D array definition:
#include <stdio.h> int main() { int twoDArray[3][4]; // 2D array with 3 rows and 4 columns // Accessing elements twoDArray[0][0] = 1; twoDArray[1][2] = 5; return 0; }
How to declare a 2D array in C:
#include <stdio.h> int main() { int rows = 3, columns = 4; int twoDArray[rows][columns]; // Declare a 2D array with dynamic dimensions return 0; }
Initializing a 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} }; return 0; }
Assigning values to a 2D array in C:
#include <stdio.h> int main() { int twoDArray[3][4]; // Assigning values for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { twoDArray[i][j] = i * 4 + j + 1; } } return 0; }
Dynamic 2D array in C programming:
#include <stdio.h> #include <stdlib.h> int main() { int rows = 3, columns = 4; // Allocate memory for a dynamic 2D array int** dynamicTwoDArray = (int**)malloc(rows * sizeof(int*)); for (int i = 0; i < rows; ++i) { dynamicTwoDArray[i] = (int*)malloc(columns * sizeof(int)); } // Access and assign values dynamicTwoDArray[1][2] = 5; // Free memory for (int i = 0; i < rows; ++i) { free(dynamicTwoDArray[i]); } free(dynamicTwoDArray); return 0; }
Multidimensional arrays in C with examples:
#include <stdio.h> int main() { int multiArray[2][3][4]; // 3D array with 2 planes, 3 rows, and 4 columns // Accessing elements multiArray[0][1][2] = 8; return 0; }
C array of strings and 2D array differences:
#include <stdio.h> int main() { char arrayOfStrings[3][10]; // Array of strings int twoDArray[3][4]; // 2D array // Assign values to array of strings sprintf(arrayOfStrings[0], "Hello"); sprintf(arrayOfStrings[1], "World"); return 0; }
Efficient ways to initialize a 2D array in C:
#include <stdio.h> int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; return 0; }