Diagnostic Quiz
Last updated
Last updated
Suppose we have the following array declaration:
Try out the following code. Which one would generate a compilation error?
See the difference between compilation error and runtime error at in Lec 03.
const
in CConsider the function declaration below:
Which of the following statement is FALSE?
In C, the const
keyword applies to the thing immediately to its left (or, if there's nothing to the left, it applies to the thing to its right).
So, in this problem, it means we cannot change any element in the array a
. But since a
is also a pointer variable (See more explanation here about why it is not merely an address due to array decay), we can set it to another array.
const long* a
means a
is a pointer to const, and any attempt to write via a
will error out. e.g. *a=10
is invalid.
This is different from long* const a
, which declares a const pointer a
that cannot be reassigned to point to another place. e.g. a=<pointer>
is invalid.
A stack frame stores function parameters and local variables.
When you access the "out of bound" index of an array, it won't generate compilation error, but it may generate runtime error or undefined behavior.