0

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

2 Answers 2

2

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

2 Comments

It should be "C99 and above", I think.
Variable-length arrays were mandatory in C 1999. C 2011 made them optional.
0

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

Comments

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.