Question

I am getting a compiler error on something I thought was very straight forward and I don't fully understand. I have a 2D array and I want to compare one of the elements to another in a switch case.

#define REV_1    {'A','B','C','D'}
#define REV_2    {'E','F','G','H'}
#define REV_3    {'I','J','K','L'}
void myfunction()
{
    char Revisions[3][4] = {REV_1,REV_2,REV_3};
    char Rev1FirstLetter = Revisions[0][0];
    char Rev2FirstLetter = Revisions[1][0];
    char Rev3FirstLetter = Revisions[2][0];

    char doesntmatter = 5;
    switch(doesntmatter)
    {
        case Rev1FirstLetter:
        {
            [....]
            break;
        }
        case Rev2FirstLetter:
        {
            [....]
            break;
        }
        case Rev2FirstLetter:
        {
            [....]
            break;         
        }
    }

}

I am getting compiler errors saying

Error: #268: declaration may not appear after executable statement in block 

on the lines

char Rev1FirstLetter = Revisions[0][0];
char Rev2FirstLetter = Revisions[1][0];
char Rev3FirstLetter = Revisions[2][0];

So do I have to use a pointer plus an offset to access these elements? I know you can assign values INTO the array like

Revisions[0][1] = 'F'; 

but I always thought that you could read values out of an array in the same way. Sorry for the beginner question, but could someone please explain to me what the best way to access the data in this 2D array would be, whether it requires a pointer and offset, or another method?

No correct solution

OTHER TIPS

Assignments are ok. However, there is an issue with switch statement since case values are not constant expressions. If I change case values to constant expressions i.e. 'A', 'B' it compiles with c99 flag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top