- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I declare a two-dimensional array in C++ using new?
A dynamic 2D array is basically an array of pointers to arrays. So you first need to initialize the array of pointers to pointers and then initialize each 1d array in a loop.
example
#include<iostream> using namespace std; int main() { int rows = 3, cols = 4; int** arr = new int*[rows]; for(int i = 0; i < rows; ++i) arr[i] = new int[cols]; return 0; }
This will create an 2D array of size 3x4. Be vary of clearing the memory in such cases as you'll need to delete the memory in the same way you allocated it but in reverse order, ie, you'll need to first delete 1D arrays then the array of pointers.
- Related Articles
- How do I declare a 2d array in C++ using new
- How to declare a two-dimensional array in C#
- How do I sort a two-dimensional array in C#
- How do I declare and initialize an array in Java?
- How do we access elements from the two-dimensional array in C#?
- How do I declare a namespace in JavaScript?
- How do I declare global variables on Android using Kotlin?
- Create new instance of a Two-Dimensional array with Java Reflection Method
- How do I declare a global variable in Python class?
- What is a two-dimensional array in C language?
- How to declare a multi dimensional dictionary in Python?
- Passing two dimensional array to a C++ function
- How to create a two dimensional array in JavaScript?
- How do I declare global variables on Android?
- C# program to Loop over a two dimensional array

Advertisements