user3086727
user3086727

Reputation: 1

Pointer to 2 Dimensional Array

I need to get access to 2 dimensional array that referenced from pointer to struct but I cannot see the values stored in this array. I used method thta was proposed here to get access via pointer to 2D Array ( as described here Create a pointer to two-dimensional array ) the structure encapsulated in shred memory erea between 2 processes. the struct contains 2D array of floats. altoght there are values stored in the first array of the 2D array , the value read in function foo is 0 . is this is the right access mode ? is the difference is due to the fact that the access is via double pointer mechanism ? ( using the Arrow notation )

typedef struct
{
    float32 Position[3];    
    float32 MODE[4];
    float32 GeneralInfo;
    float32 COLOR [8][3];
} tsInputData;

typedef float32 array_of_3_float32[3];

array_of_3_float32 *p_Color;
float32* p_Position;
float32* p_Mode;
float32* p_GeneralInfo;

void Init_Host(void* InData)
{
    pData = (tsInputData*)InData;

    p_Position = pData->Position;
    p_Mode = pData->MODE;
    p_GeneralInfo = &(pData->GeneralInfo);
    p_Color = pData->COLOR;
}

int foo()
{
    float32 var = p_Color[0][0];
}

Upvotes: 0

Views: 190

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

E.g. pointer to 2D array

float data[8][3] = { {1.2, 2.3, 4.5},{1.25, 1.5, 1.75}};
float (*a_8_3)[8][3] = &data;//pointer to float [8][3]
float (*a_3)[3] = data;      //pointer to float [3]
printf("%f\n", (*a_8_3)[1][2]);//1.75
printf("%f\n", a_3[1][1]);     //1.5

Upvotes: 0

Sergey L.
Sergey L.

Reputation: 22542

The way you did it is correct, though a bit cumbersome. You can define two-dimensional array pointers like this:

float32 (*p_Color)[3]; // pointer to array of blocks of three floats each

In C89 and above you are also allowed to use runtime variables for the sizes:

size_t num_cols;
float32 (*p_Color)[num_cols];

Upvotes: 2

Related Questions