1

I have a variable definition int (**ff)[4];, which is very bad looking. If I'm right (inferred from the fact that int (*f)[4]; is a pointer to an array of 4 int-s) this is a pointer to a pointer to an array of 4 int-s.

Now I have tried to initialize this thing but I had no success. Initializing f was simple (f = new int[5][4];), but the line ff = new int*[6][4]; is not working. Microsoft Visual Studio Community 2013 says in an error message that

a value of type "int *(*)[4]" cannot be assigned to an entity of type "int (**)[4]"

I have a very bad feeling that I really misunderstood something.

EDIT:

Sorry, that I didn't said it explicitly: What I wanted, is to allocate memory space for not only one, but for more pointers which later can point to an array of an array of 4 int-s. Of course I wanted this in one line without the help of any other definition, conversion etc.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
Bartis Áron
  • 618
  • 1
  • 10
  • 22

4 Answers4

4

I can (not) see this used only in this way:

int (*a)[4] = new int[6][4];
int (**ff)[4] = &a;

int (**ff)[4] means "a pointer to pointer to an array of four ints". This means we have to have int (*)[4] first - an lvalue, so we can take its address with operator&.

On the other hand, new int*[6][4]; allocates an array of pointers to an array of four pointers (the "outer" array decays). This is completely different type from the first one.

I had to help myself with cdecl.org on this one.

Edit:

I've just made this:

using PTR = int (*)[4];
int (**ff)[4] = new PTR; // hooray, a dynamically allocated pointer!

but can't figure a way without the type alias...

Actually, there is one: int (**ff)[4] = new (int (*)[4]), gathered from this Q&A. Don't forget that all you have now is a dynamically allocated uninitialized pointer.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • 1
    Another plan might be `typedef int A4[4]; int (**ff)[4] = new A4*[length];` and the a loop, so you allocate a 3-d array with the first 2 dimensions variable and the third dimension `4`. – M.M Feb 16 '16 at 23:20
  • @M.M I need a help out here! Can you figure out a way without a `typedef`? I've bumped into a syntax barrier. – LogicStuff Feb 16 '16 at 23:27
  • 2
    [You're not the first to wonder about this](http://stackoverflow.com/questions/26029277/why-is-new-int-3-an-error) – M.M Feb 16 '16 at 23:29
  • 1
    I forgot about the extra-parentheses version from my own question, doh – M.M Feb 16 '16 at 23:30
0

A double pointer is a pointer to a pointer to get the obvious out of the way. That means that it will hold the memory address of a pointer, again obvious. That means you initialise your pointer and only then point your double pointer to it. Such as,

int* f[] = new int[4];
int** ff = &f;

Then to access this you can do,

(*ff)[1]

This will allow you to access the information stored in the dynamic array.

NOTE: leave the [] beside f empty if the size is not known at compile time which is the reason you should be doing it this way in the first place.

Careful Now
  • 260
  • 1
  • 9
0

If you want to access elements of an array a[3][3] by pointer to pointer to array than you can do that by below code :

int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int (*p)[3]=a;
int (**ff)[3]=&p;

To print it :

int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",ff[0][i][j]);
}
printf("\n"); 
}
-3

Alan,

I think the problem here is your initial assumption is incorrect. *f[4] (or (*f)[4]) would actually be an array of 4 pointers to int, not a pointer to an array of 4 ints. Then, **ff[4] would be a pointer to an array of 4 pointers to int. I would recommend backing up a little, with this information in hand, and trying it again. Also, an assignment to the **ff would be a single pointer, and probably not an array, depending on how you are intending to use it.

                            Blake Mitchell
                            retired free lance programmer
Blake
  • 15
  • 2
  • 2
    `int* f[4]` is an array of 4 pointers to int, `int (*f)[4]` is a pointer to array of 4 ints. – Anton Savin Feb 16 '16 at 23:11
  • You don't need to sign off your answers - instead you can put that detail in your profile (click your name to get there) – M.M Feb 16 '16 at 23:20