0

This is a sample of code I'm dealing with.

switch(n){
  case 1:
      char tcol[4][100]={"Canadian Order","Achat Francais","china"};
      char trow[7][100]={"Item","Price","Qty","Total","Tax","Grand total"};
      Htable(trow,tcol,7,4,"");
      break;
  case 2:
      char tcol[3][100]={"Other column 1","2nd column"};
      char trow[4][100]={"1st row","2nd row","Row 3"};
      Htable(trow,tcol,4,3,"");
      break;
  case n:
  ...
}

Basically I'm creating a function Htable that takes the row and column names for an HTML table and the 3rd and 4th argument is the number of rows and columns.

The problem I have is the compiler thinks I'm redefining the array even though each definition is accessible only once from a switch branch. Here's the errors:

./html2.c:118: error: redefinition of ‘tcol’                          
./html2.c:110: error: previous definition of ‘tcol’ was here          
./html2.c:119: error: conflicting types for ‘trow’                    
./html2.c:111: error: previous declaration of ‘trow’ was here        

And that keeps happening for each block of code I have. The only partial solution I could come up with is the following:

char tcol[1][4][100]={"Canadian Order","Achat Francais","china"};
char trow[1][7][100]={"Item","Price","Qty","Total","Tax","Grand total"};

char trow[2][4][100]={"1st row","2nd row","Row 3"};
char tcol[2][4][100]={"Other column 1","2nd column"};

switch(n){
  case 1:
      Htable(trow[1],tcol[1],7,4,"");
      break;
  case 2:
      Htable(trow[2],tcol[2],4,3,"");
      break;
  case n:
  ...
}

Is there a simple way to do this where the C compiler accepts it without having to add fancy code to separate the strings?

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
  • 2
    1. make those static const. 2. Put each case block (after the case label) in braces. Ex. `case 1: { ...code here... } break;` – WhozCraig May 16 '19 at 02:10
  • Possible duplicate of [odd variable scope in switch statement](https://stackoverflow.com/questions/13724281/odd-variable-scope-in-switch-statement) – 1201ProgramAlarm May 16 '19 at 03:39

1 Answers1

0

Is there a simple way to do this where the C compiler accepts it without having to add fancy code to separate the strings?

Declare objects tcol, trow in separate sub-blocks. @WhozCraig

void foo(int n) {
  switch (n) {
    case 1: { // add {
      char tcol[4][100] = {"Canadian Order", "Achat Francais", "china"};
      char trow[7][100] =
          {"Item", "Price", "Qty", "Total", "Tax", "Grand total"};
      Htable(trow, tcol, 7, 4, "");
      break;
    } // add }
    case 2: { // add {
      char tcol[3][100] = {"Other column 1", "2nd column"};
      char trow[4][100] = {"1st row", "2nd row", "Row 3"};
      Htable(trow, tcol, 4, 3, "");
      break;
    } // add }
  }
}

Note that the lifetime of tcol is only to the block. Make static to last always. Yet I suspect, depending on larger code, a new approach may be needed - something fancy.


Size optimization aside: consider forming an array of pointers to strings.

      // char tcol[4][100] = {"Canadian Order", "Achat Francais", "china"};
      const char *tcol[] = {"Canadian Order", "Achat Francais", "china", NULL};

Changes also needed to Htable().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256