M
Martin Wells
Someone posted the following excerpt recently in relation to the
sizeof operator:
6.5.3.4p2: "... If the type of the operand is a variable
length array type, the operand is evaluated; otherwise,
the operand is not evaluated and the result is an
integer constant."
The first thing that occured to me was that nothing happens when you
evaluate a VLA. I mean what's the following supposed to do?
{
int arr[some_runtime_figure];
arr;
}
So then I tried to think what *other* expressions could be a VLA. My
first thought was a function, but the following won't compile for me
with a C99 compiler, it says C99 doesn't allow to return an array from
a function:
int Func(void)[5]
{
int arr[5] = {0};
return arr;
}
int main(void)
{
sizeof(Func());
}
Then I used even more of my brain and realised that I hard-coded 5
into the function signature so I wouldn't be able to return a VLA
anyway.
So I'm left wondering, what kind of expression is a VLA, but which
also DOES SOMETHING? What was the purpose of adding that little
paragraph to the Standard?
Would it not have been simpler to leave us with the universal "sizeof
doesn't evaluate its operand"?
Martin
sizeof operator:
6.5.3.4p2: "... If the type of the operand is a variable
length array type, the operand is evaluated; otherwise,
the operand is not evaluated and the result is an
integer constant."
The first thing that occured to me was that nothing happens when you
evaluate a VLA. I mean what's the following supposed to do?
{
int arr[some_runtime_figure];
arr;
}
So then I tried to think what *other* expressions could be a VLA. My
first thought was a function, but the following won't compile for me
with a C99 compiler, it says C99 doesn't allow to return an array from
a function:
int Func(void)[5]
{
int arr[5] = {0};
return arr;
}
int main(void)
{
sizeof(Func());
}
Then I used even more of my brain and realised that I hard-coded 5
into the function signature so I wouldn't be able to return a VLA
anyway.
So I'm left wondering, what kind of expression is a VLA, but which
also DOES SOMETHING? What was the purpose of adding that little
paragraph to the Standard?
Would it not have been simpler to leave us with the universal "sizeof
doesn't evaluate its operand"?
Martin