Home > database >  C linear access to 3 dimensional array
C linear access to 3 dimensional array

Time:01-10

Hello as far as I understand when I allocate 3 dimensional array I truly get linear memory that is just interpreted as 3 dimensional by defining stride.

So I want to access using linear index the 3 dimensional array, but how can I get first element using linear index - it should be possible in principle for example given int32 in order to get 11th element I should move 320 bits from beginin f the array take next 32 and interpret those bits as int - seems to be far more performant than calculate the 3 d indicies from linear index as it would require multiple divisions...

Below I tried dereferncing but still I do it incorrectly as I am here as far as I get get dereferncing to 0th element of second array not what I intend to

C code

int intsss[3][3][3] = {
                     { {1,2, 3}, {4, 5, 6}, {7,8, 9} },
                     { {10,11, 12}, {13, 14, 15}, {16,17, 18} },
                     { {19,20, 21}, {22, 23, 24}, {25,26, 27} }
  };

 std::cout << intsss[0][0][1] << std::endl;
  std::cout << **intsss[1] << std::endl;

output

2
10

CodePudding user response:

**intsss[1] is the same as **(intsss[1]). If you want to express intsss[0][0][1] in a different way, use (**intsss)[1] (but in my opinion, it's pointless).

To address your multidimensional array in a linear way, first get a pointer to the first element: &intsss[0][0][0]. Then use it like a normal pointer:

int* p = &intsss[0][0][0];
std::cout << p[1];

There are a few other equivalent ways to get a pointer to the first element: intsss[0][0] or **intsss. If you use these, you should be careful and make sure you understand how exactly pointer decay works, and make sure you get a pointer to int and not a pointer to an array. For example: intsss[0] is a 2-D array, which decays to a pointer to an array of 3 ints. Doing arithmetic on this pointer jumps 3 ints at once — probably not what you want.

Strictly speaking, such linear addressing is illegal (undefined behaviour) if you go past the end of the innermost array. But in practice, it works.

CodePudding user response:

Use below code to play-around till u get what you want.

#include <iostream>
#include <sstream>
#include <string>

void PRINT(const std::string &s) { std::cout << s; std::cout.flush(); }
template<typename...T> void say(T...t) { std::string s{}; std::stringstream ss(""); (ss<<...<<t); s=ss.str(); PRINT(s); }

int main()
{
    int intsss[3][3][3] = { { {1,2, 3}, {4, 5, 6}, {7,8, 9} },
                            { {10,11, 12}, {13, 14, 15}, {16,17, 18} },
                            { {19,20, 21}, {22, 23, 24}, {25,26, 27} } };
    int a, b, c;
    say("\n-----------\n");
    a=intsss[2][2][0];
    b=intsss[2][2][1];
    c=intsss[2][2][2];
    say("330 -> ", a, "\n331 -> ", b, "\n332 -> ", c, "\n");
    
    say("\n-----------\n");
    a=**intsss[0];
    b=**intsss[1];
    c=**intsss[2];
    say("**0 -> ", a, "\n**1 -> ", b, "\n**2 -> ", c, "\n");
    
    return 0;
}
  •  Tags:  
  • Related