My question is how exactly sizeof()
behaves when passed argument is a dynamic array variable length array.
Let's consider an example:
int fun(int num_of_chars)
{
char name_arr[num_of_chars] = {0};
/* Do something*/
return sizeof(name_arr);
}
In this example it is obvious that return value is not a compile time constant. Because the size depends on run time value of num_of_chars
.
A quote from C99 standard (6.5.3.4):
The
sizeof
operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. 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.
What I can understand from [....the operand is evaluated....] is that when the argument passed for sizeof()
is a dynamic array variable length array, sizeof()
'behaves like' a function and not as an operator.
Is my understanding right?