C
Christopher C. Stacy
Some people say sizeof(type) and other say sizeof(variable).
Why?
Why?
Christopher said:Some people say sizeof(type) and other say sizeof(variable).
Why?
Some people say sizeof(type) and other say sizeof(variable).
Why?
Christopher said:Some people say sizeof(type) and other say sizeof(variable).
Why?
Christopher said:Some people say sizeof(type) and other say sizeof(variable).
Why?
Some people say sizeof(type) and other say sizeof(variable).
Why?
Michael Mair said:You mean: One can apply the sizeof operator either to a
cast expression, i.e. (type), or to an expression, e.g
variable or *variable. Both are valid uses, in both cases
you get the size (in bytes) of the type of the expression.
Keith said:In sizeof(type), the (type) has nothing to do with cast expressions,
it's just a type name enclosed in parentheses.
Keith Thompson said:In sizeof(type), the (type) has nothing to do with cast expressions,
it's just a type name enclosed in parentheses.
Some people are right, and some people are wrong, I guess? ;-)
The syntax for unary operators is pretty clear -
sizeof unary-expression
sizeof(type-name)
Of course, putting an expression in parenthesis has no effect so both
forms are ok. Although I do not use parentheses for both forms, I
suspect that using parentheses probably cuts down on potential errors
of the type sizeof type.
Tim Rentsch said:They have nothing to do with each other, except that a type operand of
'sizeof' and a prefix that specifies casting happen to have identical
syntax, namely '( type-name )'. I expect that's more than just a
coincidence.
I find that having a unary operator which is an identifier is a
cognitive dissonance in C (I don't use the word forms of &&, ||, ! etc.
either). And if one of them needs a parenthesis, both should. Why
(sizeof x) but not (sizeof x_t)? Putting it with parentheses at least
preserves syntax with the rest of the language (it looks like a
parameterised macro or a function call).
> The sizeof operator has two forms. From the standard:
>
> 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. ....
> Some people, for reasons known only to them, insist on parenthesizing
> the expression, but
> sizeof(expression)
> and
> sizeof expression
> mean exactly the same thing.
Michael Mair said:Using parentheses around *p is a sign of confusion.
Some people say sizeof(type) and other say sizeof(variable).
Why?
Except when expression is a cast expression.
I agree. sizeof is a function, so it sould take its argument in parenthesesChris Croughton said:I find that having a unary operator which is an identifier is a
cognitive dissonance in C (I don't use the word forms of &&, ||, ! etc.
either). And if one of them needs a parenthesis, both should. Why
(sizeof x) but not (sizeof x_t)? Putting it with parentheses at least
preserves syntax with the rest of the language (it looks like a
parameterised macro or a function call).
Malcolm said:I agree. sizeof is a function, so it sould take its argument in parentheses
like other C functions.
I agree. sizeof is a function, so it sould take its argument in parentheses
like other C functions.
It is a function of the type rather than the value of its argument, so
sizeof(int) is preferable to sizeof(x).
Personally I'm sceptical of the argument that sizeof *ptr is robust to
changes in the type of ptr - anyone who changes the type of a pointer should
scan through all code that references it as a matter of course. However it
is a legitimate point..
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.