Sign in to follow this  
xidis

multidimensional array types

Recommended Posts

Given this code:
int main()
{
    int array[3][4][6];
    
    return 0;

}

What is the type of array. I believe it is a pointer to the first element which is an array of int arrays. How would you represent that in code?

Share this post


Link to post
Share on other sites
No, the type of the array is int [3][4][6]; It however has an implicit decay to the int (*)[4][6] type. So you could in theory do something like:

int (*parray)[4][6] = array;


However, such a usage is error prone. I would recommend wrapping the array in a struct or class and pass pointers or references to that instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by xidis
Given this code:

*** Source Snippet Removed ***

What is the type of array.


Well the type is exactly that a 3d array of ints, unless you mean how is it representated

Quote:
Original post by xidis
I believe it is a pointer to the first element which is an array of int arrays.


well in C++ given T foo[m][n][o]

The type of T[i][j][k] is of const T& or T& depending on the context.

The type of T[i][j] is of const T (&)[o] or T (&)[o] depending on the context.

The type of T[i] is of const T (&)[n][o] or T (&)[n][o] depending on the context.

if you not sure what the last two are, they are a reference to a (multidimensional) array.

Quote:
Original post by xidis
How would you represent that in code?


What do you mean?

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
The corresponding pointer type would be int (*)[4][6].


const int (&)[4][6] or int (&)[4][6] for C++ [grin], although you do get abit more flexibility using pointers to arrays.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this