I have been searching (google and books) for an answer to this question for a long time without managing to find any answer worth the name, so I'll try posting a thread hoping for better luck.

Can someone explain to me why this:

``````Aclass **tmp, *aclass[10];
tmp = aclass;``````

or this:

``````Aclass ***tmp, **aclass[10];
tmp = aclass;``````

is okay, while this:

``````Aclass ***tmp, *aclass[10][10];
tmp = aclass;``````

or this:

``````Aclass **tmp, aclass[10][10];
tmp = aclass;``````

is not.

What I've managed to "figure out" so far is that is has to do with a double pointer not really being the same as a pointer array, just that they happen to be (in lack for a better word) "compatible" in the first cases. But, since I obviously don't quite understand the full mechanics I can't see why they shouldn't continue to be compatible for higher order arrays.

My primary question though is whether it is possible in C++ (or C) to create a new reference to a multi-dimensional pointer array (in my example so I can continue manipulating `aclass` using `tmp` ) and if so/or not how this be done/or worked around, working only with ordinary arrays (not Array or vectors). Obviously in a case where I do not know the size of `aclass` before hand which would make the problem very trivial.
Simply put, solving this case:

``````Aclass ***tmp, *aclass[10][10];
tmp = aclass;``````

is my main concern, and any knowledge beyond that will "merely" be appreciated for what it is.

I will be bowing to who ever manages to answer this question for a very long time. ;)

3
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by t_s

When you use an array name, it's converted to a pointer to the first element. That's why this works:

``````int arr[10];
int *p = arr;``````

By a rights and purposes, an array shouldn't be compatible with a pointer and the assignment should fail. But because arr is converted to a pointer to int before the assignment, it works like this:

``````int arr[10];
int *p = (int*)&arr[0];``````

This conversion only applies to the first dimension, so even though you might expect the following to be valid, it's not:

``````int arr[10][10];
int **p = arr;``````

The rule is the same: when you use arr, it becomes a pointer to the first element. However, because the rule isn't applied recursively, what you get is a pointer to an array of 10 int, not a pointer to a pointer to int. This is the correct typing:

``````int arr[10][10];
int (*p)[10] = arr;

// Likewise with further dimensions
int arr2[10][10][10];
int (*p2)[10][10] = arr2;``````
thanks , this was really insightful !

The first half was the part I (more or less) had managed to figure out on my own although your second example put it in a more graphically appealing manner. =)

:icon_question: On to the second half, so if understand you correctly, there is in C, NO way to accomplish what I desired? (i.e. create a "arbitrary pointer" for a multidimensional array/pointer-array for later assignment as in my third example)
This assumption based on your last examples where the "arbitrary pointers" required prior knowledge about the array sizes they were being assigned to.

PS. I'm sorry if I don't use the correct terms but hopefully my meaning gets across.

>if understand you correctly, there is in C, NO way to accomplish what I desired? (i.e. create
>a "arbitrary pointer" for a multidimensional array/pointer-array for later assignment as in my
>third example)
Correct. That's not to say that you can't do it at all, you just can't do it with actual arrays due to the type mismatch. You can easily simulate a multi-dimensional array using dynamic memory, and from there you can use straight pointers. Since you mentioned C, I'll give you a C example even though this is the C++ forum:

``````int **arr = malloc ( 10 * sizeof *arr );
int **p;
int i;

for ( i = 0; i < 10; i++ )
arr[i] = malloc ( 10 * sizeof *arr[i] );

/* Use it just like a 2D array */
for ( i = 0; i < 10; i++ ) {
int j;

for ( j = 0; j < 10; j++ )
arr[i][j] = i * j;
}

p = arr; /* OK! */``````

This kind of goes without saying, because both arr and p were declared as int**, but perhaps the allocation method for simulating a multi-dimensional array will be useful to you.

Sucks to be me then I guess. =)
But that example of yours opened up my one tracked brain a little. Never thought of simulating an array with malloc, which should work just fine for a double array of pointers. I'm just surprised that there isn't a built-in (= optimized) function for doing that even in C++.

Thank you for your help.

PS. How do I do this, do I add to your rep first and then mark solved, the other way around, or just either (and does it matter which one).....or is there some FAQ I should have read? :)

Great tutorial on pointers: Narue's Eternally Confuzzled (Click Here)

C++ dynamically allocating memory:

``````int *pMyInt = new int;

...

delete pMyInt;``````

Great tutorial on pointers: Narue's Eternally Confuzzled (Click Here)

C++ dynamically allocating memory:

``````int *pMyInt = new int;

...

delete pMyInt;``````

Already seen that page, but I thought it was a little too..basic for my needs, or so I thought when I read through it, maybe I missed something? And I'm also more intressted in pointer to object etc. Thanks though...

>I'm just surprised that there isn't a built-in (= optimized) function for doing that even in C++.
There is, it's called the std::vector class. ;)

>PS. How do I do this, do I add to your rep first and
>then mark solved, the other way around, or just either
Marking a thread as solved and giving rep are independent. Do either, neither, or both as you feel necessary.

>I thought it was a little too..basic for my needs
It's a basic overview, so no worries.

>I'm also more intressted in pointer to object etc.
Pointers are very regular. Once you know a few basic rules about them, you can apply those rules everywhere. A pointer to an int is no different fundamentally from a pointer to an object in terms of how the pointer works.

Will do.
Mumble mumble..vectors..yes...anyway, thanks for all the replies.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.