int *A [10];
A[2][3]=15;
This statement gives no compile time error, yet when I run the program it gives a runtime error. Why?
Edit: I specifically want to know why there is no compile time error?
int *A [10];
A[2][3]=15;
This statement gives no compile time error, yet when I run the program it gives a runtime error. Why?
Edit: I specifically want to know why there is no compile time error?
This statement gives no compile time error in c
Because there is no syntactical error or constraint violation that compiler should be judging. C does not do any bound-checking for arrays (or pointer arithmetics).
You'll be perfectly allowed to write a code which makes use of invalid memory (example: dereference an invalid memory location) but in case, the compiler has produced a binary for such code, running the binary would invoke undefined behaviour.
yet when I run the program, it gives a runtime error
In your code,
int *A [10];
A is an array of 10 int *s, and they are not initialized explicitly. Looking from the snippet, it appears A is not in global scope, i.e., not having static storage, so the contents of each of those pointers are indeterminate.
So, later in process of writing A[2][3]=15;, you're trying to access A[2] (a pointer), which points to an invalid memory. This invokes undefined behavior.
A[2] is a valid element of the array A... The wording in your last sentence can be betterindeterminate and attempt to use indeterminate "value" leads to UB, right?Because accessing the uninitialized pointer A[2] invokes undefined behavior, which means anything is allowed to happen as far as the C standard is concerned.
why there is no compile time error
Because the standard doesn't require a diagnostic (such as a compile error) to be issued in this case.