Reputation: 4687
I am new to C++ and I found this code in a tutorial. I am perplexed to find double stars instead of a single star used for defining a pointer. Is this a substitute for two dimensional array? If yes how?
For example:
int* A = new int[5];
makes an array strip , how does it happen in int **A
?
#include <iostream>
using namespace std;
int main () {
int **A;
A = new int*[10] ;
for (int j=0; j<20; j++)
A[j] = new int[20] ;
for (int i=0; i<10; i++)
for (int j=0; j<20; j++)
A[i][j] = (i+1)*(j+1);
for (int i=0; i<10; i++) {
for (int j=0; j<20; j++)
cout << A[i][j] << " ";
cout << endl;
}
cout << endl;
}
Upvotes: 1
Views: 1430
Reputation: 34605
int **A;
means it is a pointer to a pointer. This is often used to represent a two-dimensional array. In you program -
int *A; A = new int[10] ;
for (int j=0; j<20; j++)
A[j] = new int[20] ; // A[j] means dereferencing to the location itself. So, it should hold `int`
// as stated but not `int*` that is returned by new operator.
A
is pointer that points 10 memory locations that can hold int
. And when you say, A[j]
you are actually dereferencing to location. So, it holds an int
not an int*
. Also, there is a overflow since the number of locations A
points to 10 but not 20 memory locations.
Edit 1: OP has edited the program. It's not the same as was earlier.
Now, the program is fine, with exception of returning resources back to free store.
int **A;
A = new int*[10] ;
A
is a pointer to pointer. So, a should hold the address of a pointer or an array that can hold address of pointers. So,
A = new int*[10] ;
A
holds the beginning address of an array of pointers whose size is 10
. Now this is still wrong.
for (int j=0; j<20; j++)
A[j] = new int[20] ; // Because `A[j]` is invalid from 10-19. There is no such index where A[j] can dereference to. So, j should run from 0 to 10 instead.
With that correctected -
A[j] = new int[20] ;
Now each pointer is pointing to 20 int locations. So, there are 10*20 elements in the array.
This diagram of a 2*3 dimensional array should give you an idea. Also, you should consider @Nawaz answer of deallocating the array, since resources are managed by programmer.
Upvotes: 3
Reputation: 361254
Yes. That is two-dimensional array: more precisely, pointer to pointer!
And you've to deallocate it once you're done with it. This is how you've to delete it:
for (int j=0; j<20; j++)
delete[] A[j];
delete[] A;
It's good that you're experimenting with it. It would be also good if you also get familiar with std::vector
and experiment with it as well.
Example of std::vector
#include <vector>
std::vector<std::vector<int> > A(10, vector<int>(20));
for (int i=0; i<10; i++)
for (int j=0; j<20; j++)
A[i][j] = (i+1)*(j+1);
for (int i=0; i<10; i++) {
for (int j=0; j<20; j++)
cout << A[i][j] << " ";
cout << endl;
}
That is, if you use std::vector
, then you don't have to worry about allocation and deallocation of memory!
Demo at ideone: http://ideone.com/LEOBe
Upvotes: 4
Reputation: 983
The first thing you need to know is the difference between a pointer and an array in C and C++. An array type array[size]
is size consecutive cells of sizeof(type) data. A pointer is a variable that contains an address. The confusion comes from the fact that a pointer is used to store the address of the first cell of a dynamically allocated array (malloc in C, new [] in C++).
So, what is the difference between int array[10]
and int* pi
? array is a memory area of the size of 10 ints and pi is a variable which contains the address of an int.
To declare a two dimensions array you can type : int array2d[10][10]
or you can declare a "pointer to pointer" such as int** ppi
. Don't forget that id you use the second one, you will have to allocate both the first (an array of pointer) and the second (your actual array of ints) dimensions.
Upvotes: 2
Reputation: 10939
To create a 2D array, you can either create an array of arrays (this results in a jagged array), or you can create a 1D array and reference it in such a way that it's like a 2D array (this second method is kind of awkward).
// array of arrays
int **A = new int*[5]; // an array of potentially 5 int arrays
for( int i = 0; i < 5; ++i)
{
A[i] = new int[5];
}
// access the array using two brackets
A[0][0] = 1; // element in the zeroth row and zeroth col = 1
A[1][0] = 2; // element in the first row and zeroth col = 2
// you must delete the inside arrays first before deleting the array of arrays
for(int i = 0; i < 5; ++i)
{
delete[] A[i];
}
delete[] A;
Upvotes: 1
Reputation: 18712
The two starts mean that it is a pointer to a pointer. This line:
A = new int*[10];
Allocates space for 10 int*
values. Later, each of those int*
values are initialized with:
for (int j=0; j<20; j++)
A[j] = new int[20];
The result is a 'jagged array', or an array of arrays. Hope that helps.
Upvotes: 1