Question

I don't get it what this means. I already thought this could mean code as in my code snippet of this Question:

Skipping switch cases via false loop is a valid operation?

But as the answerers just where going to improve the code and ignored my question about the c99 quote, I'm going to ask this here now explicitly:

If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.135)

And here's the footnote:

135) That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch that is in the block containing the declaration.

could any one be so kindly and explain it to me in other words? Thanks for the effort.

Was it helpful?

Solution

First note that this sentence applies only for identifiers of variably modified type, that is a type that has a dynamic array dimension somewhere in its description. For n a variable something like

double a[n];
unsigned (*B)[n][n];

The objects that are associated to this kind of identifier have a special rule for their life time, it only starts at the point of declaration, whereas for other types it starts at entering the scope.

The paragraph that you are citing is to ensure that all the case of the switch statement has the same property according to that life time on such an object. Either the life of the object has already started, before any of the cases, or it only starts after any of the case (or default) labels.

So in essence it just indicates that you shouldn't mix usage of VLA (or similar) and jump statements, because you can't know what the size is to be and where the memory for the array (if any) has to be allocated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top