
- 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
Passing two dimensional array to a C++ function
C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index. There are three ways to pass a 2D array to a function −
Specify the size of columns of 2D array
void processArr(int a[][10]) { // Do something }
Pass array containing pointers
void processArr(int *a[10]) { // Do Something } // When callingint *array[10]; for(int i = 0; i < 10; i++) array[i] = new int[10]; processArr(array);
Pass a pointer to a pointer
void processArr(int **a) { // Do Something } // When calling: int **array; array = new int *[10]; for(int i = 0; i <10; i++) array[i] = new int[10]; processArr(array);
example
#include<iostream> using namespace std; void processArr(int a[][2]) { cout << "element at index 1,1 is " << a[1][1]; } int main() { int arr[2][2]; arr[0][0] = 0; arr[0][1] = 1; arr[1][0] = 2; arr[1][1] = 3; processArr(arr); return 0; }
Output
This will give the output −
element at index 1,1 is 3
- Related Articles
- Passing an array to a C++ function
- Passing a 2D array to a C++ function
- Split one-dimensional array into two-dimensional array JavaScript
- How to create a two dimensional array in JavaScript?
- C# program to Loop over a two dimensional array
- How to declare a two-dimensional array in C#
- How to create a two-dimensional array in TypeScript?
- Transpose of a two-dimensional array - JavaScript
- Apply the ufunc outer() function to all pairs of Two-Dimensional Array in Numpy
- Golang Program to Multiply two Matrices by Passing Matrix to a Function
- Swift Program to Multiply two Matrices by Passing Matrix to a Function
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- Golang Program to Add Two Complex Numbers by Passing Class to a Function
- What is a two-dimensional array in C language?
- Get the Inner product of a One-Dimensional and a Two-Dimensional array in Python

Advertisements