-1

Using xcode 5.1.1, it shows the message Thread 1:EXC_BAD_ACESS(code=1,adress=0x7ffae2c04c48) on the line 'printf("digite o %i horário :",i+1);'

Does someone have any idea about that? I tried to deactivate lldb, but it just didn't work.

#include <stdio.h>

int main (){

    struct hora {
        int h;
        int m;
        int s;
    };

    int i, a;

    struct hora lista[i];

    for (i = 0; i<5; ++i) {
        printf("digite o %i horário :",i+1);
        scanf("%i:%i:%i",&lista[i].h,&lista[i].m,&lista[i].s);
    }
    for (a=0; a<5; ++a){
        printf("o horário %i é %i:%i:%i", a+1,lista[a].h,lista[a].m,lista[a].s);
    }
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    `i` is uninitialized here: `struct hora lista[i];` It has indeterminate value and it leads to *undefined behaviour*. – P.P Aug 10 '15 at 15:26

1 Answers1

1

Your code shows undefined behaviour., as in the statement

 struct hora lista[i];

you're using i unitialized.

To elaborate, i being an automatic storage type local scope variable, it is not initialized implicitly. Unless initialized explicitly, the content of i in un-deterministic. Using the value is hence UB.

You may want to change that to

 struct hora lista[5];

to make it proper.

That said, as a note, int main() is not recommended in C standard, use int main(int argc, char * argv[]) or at least, int main(void).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261