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.

5
  • 3
    You see this variable, the first thing that comes to your mind is how to initialize it? I'd dump it :) Commented Feb 16, 2016 at 22:48
  • 1
    I just experimented with the language and wanted to understand better this part of it. Commented Feb 16, 2016 at 22:49
  • See std::array, std::vector. Commented Feb 16, 2016 at 22:50
  • @Bartis Point taken. Commented Feb 16, 2016 at 22:50
  • There are hundreds of different ways to properly initialize int (**ff)[4]. But there's no meaningless answer to your question until you explain what you want to use that int (**ff)[4] for. What are you trying to do? For example, you can initialize that ff with your own &f: int (**ff)[4] = &f;. Done. Does this help you? No one knows.... Commented Feb 16, 2016 at 23:38

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 I need a help out here! Can you figure out a way without a typedef? I've bumped into a syntax barrier.
I forgot about the extra-parentheses version from my own question, doh
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.

Comments

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"); 
}

Comments

-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

2 Comments

int* f[4] is an array of 4 pointers to int, int (*f)[4] is a pointer to array of 4 ints.
You don't need to sign off your answers - instead you can put that detail in your profile (click your name to get there)

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.