3

I know this question was probably asked before, but I don't understand it in this context:

Here's the code I'm trying to examine, I'll comment it. Please let me know where I'm wrong

int **A;                    // declaring a pointer to a pointer

A = new int*[n];            // assigning that pointer to a newly-allocated 
                            // space (on the heap) for an array
                            // of [size n] of pointers to integers

for (i = 0; i < n; ++i)     // looping from 0 to n-1
    A[i] = new int[n];      // assigning each slot's pointer to a 
                            // new array of size n?

for (i = 0; i < n; ++i)     // loop through all the rows
    for (j = 0; j < n; ++j) // loop through each column for the current row
        A[i][j] = 0;        // assign the value to 0

Please let me know where I'm wrong. I don't understand anything from A = new int*[n]; I'm just trying to figure it out using common sense but I'm having troubles.

Thank you!

3
  • What you described in your comments is correct. What is not working? Are you trying to use a double pointer where a true 2D array is expected or what? Commented Jul 31, 2013 at 4:26
  • It is working. I just don't understand how 2 pointers (one pointing at the other) correspond to a 2-dimensional data structure... I need a diagram or something Commented Jul 31, 2013 at 4:27
  • Well, the first index is the row index (it's an array of pointers to the starting address of a row), the second index is the index of the column in that row. Commented Jul 31, 2013 at 4:29

3 Answers 3

2

Your code and comments are correct. To clear up the confusion about pointers:

  • A 2D array is simply an array of arrays.
  • In order to allow the arrays to have a dynamic size (i.e. size n not known at compile time), an array of pointers is created. Each pointer in the array itself points to an array of ints.
  • The resulting structure is very similar to an array of arrays - it's an array of pointers to arrays. The pointers are there simply because you cannot have e.g. int a[n][n]; at compile time because the size is not known.

Here's a diagram of this:

> [       ]      //">" is a pointer, pointing at an array ([ ])


    [ ]  [ ]  [ ]
> [  ^    ^    ^ ]  // The array is an array of pointers "^", each of which points to an array
Sign up to request clarification or add additional context in comments.

7 Comments

"A 2D array is simply an array of arrays." - it's important to point out that as such, OP does not have a 2D array.
So, to summarize, in my case, A is a pointer to an array of pointers to arrays? Is that correct?
@H2CO3: He does, logically. It just doesn't happen to match with the C terminology.
@Mehrdad Well, we could argue. If you find yourself competent and entitled to override C(++) terminology, then go ahead :)
@OleksiyDobrodum Essentially yes. In fact, A is a pointer to the first element of an array of pointers to first elements of arrays :)
|
2

What's an "array"? It's a block of memory, represented by its address.

If you want a 2-dimensional array, then you need lots of those blocks -- and you need to know where each of them is located. That means you need an array of addresses, or in other words, an array of int* -- which gives you an int**.

What new int*[n] does is that it allocates memory for an array of addresses, and inside each of them, you go and put the address of an array of ints, allocated via new int[n].

2 Comments

Well, the term "multidimensional array" usually refers to arrays declared like T arr[rows][cols];. They are not the same as a pointer-to-pointer.
@H2CO3: Well, a single-dimensional isn't the same as a pointer either, but knowing that doesn't really help the OP understand the basics of what's going on yet.
2

So basically what you have here is an array of pointers to array. In other words, you have an array of pointers in which each pointer in the array points to another array.

here is a picture I found that illustrate this: enter image description here

The '[]' operator makes you access an element in an array using it index. so A[i] is accessing the i's element in the A array.

 A[i] = new int[n];  

here you make the i's pointer in an array to point to a new array.

And so what A[i][j]
actually means is that in A[i][j] you are accessing the j's element in the array that the i's element in A points to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.