Declaring Variables *solved*

Umm hey,s o I was wondering. I've been doing some arduino stuff for a couple months now, and a friend of mine linked me a program of his that kinda made me wonder a bit when I was trying to read it.

So up in the beginning of the program, he declares a variable

Int Index = 0;

Now with what I thoughts, that means its a global variable now that can be used in any segment of code (setup, loop, loops within the loop) and all. and that works for me.

But now here's where it gets odd to me.

Later on he starts a while loop designed to take your key-presses on a keypad, put them into an array, convert that array into a string, and then set that string as a filename to go to an SD card. Fine, program works. But at teh beginning of the loop we see again.

Int Index;

Shouldn't that cause a compiling error since hes declaring the same variable twice, once in a global scope and once within the scope of this while loop?

PS sample of that segment of code here

// before setup where I assumed global variables were created, the other 2 variables are used within a timing section of the program hes making.

long hrD = 0;            // Variable used for storing hours when being input from keypad
char hrD1[3];            // Used to convert keypresses into a number for arduino to use
int index = 0;           // Used to shift through arrays



// within setup, right after starting an LCD screen

  int index;   
  b = 0;
  c;

  while(b == 0){
    c = 0;
    index = 0;
    key = keypad.getKey();  //get key reading
      if (key == '*' ){//if * move to input filename, else skip it adn move on to next
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Ent date: mmddyy");
        lcd.setCursor(0, 1);
        lcd.print("* to cont");
        while(c == 0){
          key = keypad.getKey();  
          if(key == '*'){
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("file name:");
            while(index != 6)
            {
              key = keypad.getKey();
              if(key != NO_KEY)
              {
                name[index] = key;
                lcd.setCursor(index, 1);
                lcd.print(name[index]);
                index++;
                }
              } 
              delay(500);
              lcd.setCursor(0, 0);
              lcd.print("correct? *=Y,#=N");
              index1 = 0;
              while(index1 != 1){
                  key = keypad.getKey();
                  if(key == '*')
                  {
                     index1 = 1;
                     c = 1;
                     b = 1;
                     delay(500);
                     }
                  else if(key == '#'){
                     index1 = 1;
                     c = 1;
                     b = 0;
                     lcd.clear();
                     lcd.setCursor(0, 0);  
                     lcd.print("Enter File Name");
                     lcd.setCursor(0,1);
                     lcd.print("* cont, # skip");
                     delay(500);
                     }
              }
          }
        }
      }
    else if(key == '#'){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("file name dflt");
      lcd.setCursor(0, 1);
      lcd.print(name);
      b = 1;

Sorry if this seems mad newbie of a problem. It doesn't seem to cause any problems with the program (also theres a global B and C running around and then another one in this filename loop and another in another loop as well). Its was just driving me a little crazy when reading it and trying to follow what different vars were doing.

No, that doesn't cause any problems at all.
Within the scope of the new variable, the compiler uses that value, when it goes out of scope, it uses the global value.
Not really recommended, because as you've seen it can cause confusion for the reader, but not the compiler.

alright thanks, was driving me up a wall looking at the program, theres index running around in 6 separate loops. I realize every time its used to move through the positions in different arrays, but damn, somethign else for naming would be helpful.

Ok then, marking solved.

I realize every time its used to move through the positions in different arrays, but damn, somethign else for naming would be helpful.

It is fairly typical to name the array index variable index. Declaring a global variable and a local variable of the same name, though, IS to be avoided. That will drive you round the bend.