I have the following C header file:
#ifndef __MATRIX_H__
#define __MATRIX_H__
struct matrix_t
{
uint8_t col1;
uint8_t col2;
uint8_t col3;
};
extern const struct matrix_t matrix_a[3];
extern const struct matrix_t matrix_b[3];
extern const struct matrix_t matrix_c[3];
#define MATRIX_A_COL_1(row) matrix_a[row].col1
#define MATRIX_A_COL_2(row) matrix_a[row].col2
#define MATRIX_A_COL_3(row) matrix_a[row].col3
#define MATRIX_B_COL_1(row) matrix_b[row].col1
#define MATRIX_B_COL_2(row) matrix_b[row].col2
#define MATRIX_B_COL_3(row) matrix_b[row].col3
#define MATRIX_C_COL_1(row) matrix_c[row].col1
#define MATRIX_C_COL_2(row) matrix_c[row].col2
#define MATRIX_C_COL_3(row) matrix_c[row].col3
#endif
And the following C file:
#include "matrix.h"
const struct matrix_t matrix_a[3] =
{
{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}
};
const struct matrix_t matrix_b[3] =
{
{ 10, 11, 12},
{ 13, 14, 15},
{ 16, 17, 18}
};
const struct matrix_t matrix_c[3] =
{
{ 19, 20, 21},
{ 22, 23, 24},
{ 25, 26, 27}
};
Note: because my original code have 10 times more fields on matrix_t
, so for the sake of simplicity I created this example.
So my question here is: I will need the macros from matrix_a
to be used today, but tomorrow I will need the matrix_b
but they are not only 3 so I can't change everything by hand, they are several. Is it possible to make some "general" macros, instead of defining all macros and changing the code when I need to use another? And how?
Should be something like this?
...
if matrix_x == A
#define MATRIX_X_COL_1(row) matrix_a[row].col1
#define MATRIX_X_COL_2(row) matrix_a[row].col2
#define MATRIX_X_COL_3(row) matrix_a[row].col3
else if matrix_x == B
#define MATRIX_X_COL_1(row) matrix_b[row].col1
#define MATRIX_X_COL_2(row) matrix_b[row].col2
#define MATRIX_X_COL_3(row) matrix_b[row].col3
else if matrix_x == C
#define MATRIX_X_COL_1(row) matrix_c[row].col1
#define MATRIX_X_COL_2(row) matrix_c[row].col2
#define MATRIX_X_COL_3(row) matrix_c[row].col3
...
Is something like this possible to do?
Thank you.
Best regards, Ricardo Neves.