
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to define multi-dimensional arrays in C/C++?
Array is a collection of same type of elements at contiguous memory location. The lowest address corresponds to the first element while highest corresponds to last element. Array index starts with zero(0) and ends with the size of array minus one(array size - 1). Array size must be integer greater than zero.
Let us see an example,
If array size = 10 First index of array = 0 Last index of array = array size - 1 = 10-1 = 9
Multi-dimensional arrays are arrays of array. The data is stored in tabular form in row major order.
Here is the syntax of multi-dimensional arrays in C language,
type array_name[array_size1][array_size2].......[array_sizeN];
Here,
array_name − Any name given to an array.
array_size − Size of array.
The following is how you can initialize a multi-dimensional array
type array_name[array_size1][array_size2].......[array_sizeN]; = { {elements} , {elements} , ... {elements} }
Here is an example of multi-dimensional array in C language,
Example
#include <stdio.h> int main () { int arr[2][3] = { {5,2,3}, {28,8,30}}; int i, j; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 3; j++ ) printf("arr[%d][%d] = %d\n", i, j, arr[i][j] ); } return 0; }
Output
Here is the output
arr[0][0] = 5 arr[0][1] = 2 arr[0][2] = 3 arr[1][0] = 28 arr[1][1] = 8 arr[1][2] = 30
- Related Articles
- How to define multi-dimensional arrays in C#?
- How to initialize multi-dimensional arrays in C#?
- How do we use multi-dimensional arrays in C#?
- C++ Program to Add Two Matrix Using Multi-dimensional Arrays
- C++ Program to Multiply Two Matrix Using Multi-dimensional Arrays
- Multi Dimensional Arrays in Javascript
- Dump Multi-Dimensional arrays in Java
- Flattening multi-dimensional arrays in JavaScript
- Sort in multi-dimensional arrays in JavaScript
- Does Java support multi-dimensional Arrays?
- How to initialize two-dimensional arrays in C#?
- How to Map multi-dimensional arrays to a single array in java?
- How to access elements from multi-dimensional array in C#?
- How to define jagged arrays in C#?
- How to define param arrays in C#?

Advertisements