0

I'm using MingW as my compiler. I have declared a variable, strl, but it says that it wasn't declared at the line right under it.

struct idstruct {
    char * path;
    lua_State **state;
};

idstruct ids[] = {};
int nids = 0;

static char* getPath(lua_State **state) {
  std::cout << state;
  for (int on = 0; on < nids; on++)
    idstruct strl = ids[on];
    if (strl.state == state) {
      return strl.path;
    }
  }
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
user3103398
  • 143
  • 9
  • 1
    Zero-length arrays are illegal. It's also illegal indexing a zero-length array with any index if it happens to compile in the first place. – chris Apr 16 '14 at 00:25
  • 2
    At this stage of your career, it is more likely that the compiler is right than you are. That even happens after you've been programming a decade or two or three, [sometimes](http://stackoverflow.com/questions/23020208/why-is-gcc-4-8-2-complaining-about-addition-under-strict-overflow), though generally you feel that the compiler is wrong less frequently after a few years. – Jonathan Leffler Apr 16 '14 at 00:29

2 Answers2

4

You're missing a curly brace at the start of your for loop's body. By the time you reach the following if statement, strl has gone out of scope.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jaket
  • 9,140
  • 2
  • 25
  • 44
4

You've left off a brace on your for loop, so it's just a one-liner, despite your indentation. Thus the variable is not in scope in the if statement below it.

Try this:

  for (int on = 0; on < nids; on++) {  // add a brace here
    idstruct strl = ids[on];
    if (strl.state == state) {
      return strl.path;
    }
  }  // and here
Brian Neal
  • 31,821
  • 7
  • 55
  • 59