1

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;

} 
Chris Smith
  • 18,244
  • 13
  • 59
  • 81
Frustrated Coder
  • 4,417
  • 10
  • 31
  • 34
  • Maybe you will find this interesting : http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c –  Mar 12 '11 at 18:22

6 Answers6

4

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

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • hi nawaz, what exactly is std::vector? and how can i utilize it? can u give me a sample code? thanks for replying buddy! – Frustrated Coder Mar 12 '11 at 18:19
  • and by the way if I want to delete it why should j go from 0 to 20? – Frustrated Coder Mar 12 '11 at 18:25
  • 3
    @Frustrated Coder - I find that mastering the standard libraries to a language is hands down the easiest way to learn a language. You're on the right track! – corsiKa Mar 12 '11 at 18:27
  • 1
    Just a caution - C++ *has* something called multi-dimensional arrays, and this isn't one of them. A vector of vectors, and an array of pointers to arrays, can both *represent* a two-dimensional array, and both allow the `[][]` syntax, but that isn't C++'s name for them. – Steve Jessop Mar 12 '11 at 22:03
3

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.

2*3 Array Lay Out

Mahesh
  • 34,573
  • 20
  • 89
  • 115
2

It's a pointer to a pointer. In this case, it's a 2D array.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

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.

Opera
  • 983
  • 1
  • 6
  • 17
1

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.

Chris Smith
  • 18,244
  • 13
  • 59
  • 81
1

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;
helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • why is it required to do delete[] A[i] before delete[] A...what exactly is deleted when I do delete[] A[i]. Suppose i did delete[] A[1];? – Frustrated Coder Mar 12 '11 at 18:32
  • @Frustrated Coder - The sample diagram I posted should answer the question. If you do `delete[] A[1];` only first row is deallocated and rest stay there. – Mahesh Mar 12 '11 at 19:42