Better alternatives for switch statements

You can create a lookup table like this:

double *pointers[26][2] = {
    { p_YZ_L, p_YZ_R },
    ...
};

Then your function becomes much simpler:

double* getPointer(int plane, int direction) {
    if ((plane >= 0) && (plane < 26) && (direction >= 0) && (direction < 2)) {
        return pointers[plane][direction];
    } else {
        return NULL;
    }
}

If you are just tired of typing, yu can use the preprocessor, e.g.:

#define PLZ(dir) if(!dir)return(p_YZ_L);else if(dir==1)return(p_YZ_R);else return 0;

Not quite sure, but maybe you want this:

struct
{
  double dir[2];
} directions[26] =
{
  { p_YZ_L, p_YZ_R},
  { ..., ... },           // 25 pairs of options here
  ...            
};

double* getPointer(int plane, int direction) {
  return  &directions[plane].dir[direction];
}

More tests need to be added to be sure that plane and direction are within required bounds.

Tags:

C