1

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];
    }
}

2 Answers 2

1

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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];
    }
}

4 Comments

You might want to try out those pointer declarations, they are not quite correct.
Performance wise, is this faster or actually accessing by ccc[i][j][0]?
@BlackCat Why don't you try both ways, and benchmark it to find out? Both with and without optimizations.
@JoachimPileborg Yes sorry got myself confused

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.