-1

Sorry for the bad title and i am new to the C but here is the part i don't understand.

I have a simple struct;

struct st{

        int a;
        int b;
};

and i am creating array of struct and indexing values to variable 'a' in main;

int main(){
         struct st st_arr[2];
         st_arr[0].a = 5;
         st_arr[1].a = 10;
         st_arr[4].a = 20;

         printf("%d %d %d\n", st_arr[0].a, st_arr[1].a, st_arr[4].a);
}

i have assigned 2 array of structs but it lets me index 4th of the struct why is that? Isn't it suppose to give me an error?

the output is:

5 10 20

1 Answers1

1

C does not provide a(ny) bound checking by default.

Accessing out of bounds (i.e., invalid memory) is defined to have undefined behaviour.

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