R
Richard
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
Frederick Gotham said:Richard posted:
If "x" is a type, then the parentheses are mandatory:
int i = sizeof(int); /* OK */
int i = sizeof int; /* Compile Error */
If "x" is an expression, then "sizeof" works just like any other unary
operator:
&x = address of x
!x = logical inversion of x
sizeof x = size of x
I omit redundant parentheses wherever possible in my code, and so I
write:
Richard said:[snip]Frederick Gotham said:Richard posted:
If "x" is a type, then the parentheses are mandatory:
int i = sizeof(int); /* OK */
int i = sizeof int; /* Compile Error */
So at the end of the day "sizeof x" and "sizeof(x)" are identical?
Could someone point me to why "sizeof x" is/isnt preferable to
"sizeof(x)",
Richard Heathfield said:Richard said:
Would you agree that, for most purposes, j + k is preferable to (j) + (k) ?
Lew Pitcher said:Richard said:[snip]Frederick Gotham said:Richard posted:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
If "x" is a type, then the parentheses are mandatory:
int i = sizeof(int); /* OK */
int i = sizeof int; /* Compile Error */
So at the end of the day "sizeof x" and "sizeof(x)" are identical?
Nope.
At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type
At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type
Richard said:I have no idea if you are being a smartass there or not. I fail to see
the relation.
Lew said:Richard said:[snip]Frederick Gotham said:Richard posted:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
If "x" is a type, then the parentheses are mandatory:
int i = sizeof(int); /* OK */
int i = sizeof int; /* Compile Error */
So at the end of the day "sizeof x" and "sizeof(x)" are identical?
Nope.
At the end of the day,
sizeof(x)
works under all conditions, and
sizeof x
only works if x is not a type
Richard said:I have no idea if you are being a smartass there or not. I fail to see
the relation.
Parenthesis are used either for function calls, flow control or for
enforcing order of operations.
Sarcastic said:Oh, and typecasting! I forgot.
I only have a passing knowledge of C so forgive my ignorance, is the
reason we do sizeof (int) because we're doing some manner of
typecasting there?
Chris "strunk and white I'm not" Dollin
Sarcastic Zombie said:If I'm not mistaken...
The relation is that in this case, sizeof is essentually a plain old
operator. Thus sizeof(x) is the same thing as (x)++ or (j) + (k).
Chris Dollin said:"Omit needless brackets".
It makees sense to me to type it so:
x += sizeof(y);
I guess at the end of the day I "see" sizeof as a function - regardless
of the fact it isnt.
Frankly I think
x=x+sizeof q - 3
is horrible and confusing.
Frederick Gotham said:Richard posted:
It's horrible and confusing because your way of thinking is flawed. If
you
think of "sizeof" as a function rather than as an operator, then you won't
understand:
int x;
sizeof x;
And when I first saw it this way I didnt : I had to look it up.
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.