BlackCat
BlackCat

Reputation: 329

3D multidimensional array to "row" pointer

I was wondering how i can access multidimensional rows in a 3D via pointer like this:

int ccc[8][7][2] = ....;

for(int i=0;i<8;i++)
{
    int** cc_i = ccc[i];
    for(int j=0;j<7;j++)
    {
        int* c_j = cc_i[j];
        int th0 = c_j[0];
        int th1 = c_j[0];
    }
}

Upvotes: 1

Views: 63

Answers (2)

john
john

Reputation: 88017

Like this

int ccc[8][7][2] = ....;

for(int i=0;i<8;i++)
{
    int (*cc_i)[2] = ccc[i];
    for(int j=0;j<7;j++)
    {
        int *c_j = cc_i[j];
        int th0 = c_j[0];
        int th1 = c_j[0];
    }
}

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409442

You can't, because a pointer to a pointer is not the same as an array of arrays. The layout in memory is radically different.

You can however declare e.g. cc_i as a pointer to an array, like

int (*cc_i)[2] = ccc[i];

Upvotes: 1

Related Questions