Re: A couple of things from H&S
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 09 Sep 2009 23:36:25 -0700
arnuld <sunrise@xxxxxxxxxxxxxxx> writes:
On Wed, 09 Sep 2009 00:38:06 -0700, Keith Thompson wrote:
...SNIP....
And even if you have an expression as the operand of sizeof, an
expression of array type decays to a pointer to the array's first
element *unless* it's in one of three contexts: the operand of sizeof,
the operand of unary "&", or it's a string literal used to initialize an
array.
Three contexts, I understand better with examples:
1) sizeof(int arr[N++]);
``int arr[N++]'' is a declaration of an object named "arr", or it
would be if it had a semicolon and appeared by itself.
I think you mean:
sizeof (arr[N++])
where arr[N++] is an array type (specifically a VLA type). Here N++
is evaluated, with the side effect of modifying N.
Note that the operand of sizeof is a parenthesized type name, not an
expression. The operand of sizeof can be an expression of VLA type:
int n = 42;
int vla[n];
sizeof vla;
but I'm not sure it's possible for such an expression to have side
effects -- meaning it doesn't matter whether it's evaluated or not.
2) & (int arr[N++]);
Unary "&" can apply only to an expression, which must be an lvalue.
Here you're trying to apply it to a declaration. Even if you drop the
"arr", you're still applying it to a type, which is illegal.
Try this:
int arr[N++];
&arr;
N++ is evaluated just once, when the type is created. &arr just gives
you the address of arr, which is of a pointer-to-array type.
3) const char* p = "This is CLC";
(3) is quite obvious, p is the pointer. A string literal is not an array,
so there is no confusion regarding thaat there will be no conversion to
pointer from string literal.
No, a string literal *is* an expression of array type. In the above
declaration, this expression is implicitly converted to char*,
yielding a pointer to the first character of the array object.
Try this:
const char arr[] = "This is CLC";
This is the third case where an array expression isn't converted to a
pointer: when it's used to initialize an array.
What happens in case (1) and (2) ?
Your compiler complains.
This is from page 222 of H&S 5:
size_t y = sizeof(int (*)[f(n)]);
The function call may or may not be performed. Its seems just like a VLA
where it is undefined if its evaluated or not. If size of VLA affects the
result of sizeof then it will definitely be evaluated.
I never understood how can size of array affect the result of sizeof when
sizeof is only interested in knowing the type, not the number of
elements. How can you explain that ?
Because the numer of elements is part of the type.
int[10], int[20], and int[N] are three distinct types.
think these all will return the
same value:
sizeof( int arr[N++]);
sizeof( int arr[M]);
sizeof( int arr[++Z]);
These are all syntax errors, but if you drop the "arr" from each of
them, their values will depend on the values of N, M, and Z.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: A couple of things from H&S
- From: Ben Bacarisse
- Re: A couple of things from H&S
- References:
- A couple of things from H&S
- From: Frank
- Re: A couple of things from H&S
- From: Keith Thompson
- Re: A couple of things from H&S
- From: arnuld
- Re: A couple of things from H&S
- From: Keith Thompson
- Re: A couple of things from H&S
- From: arnuld
- A couple of things from H&S
- Prev by Date: Re: Schildt
- Next by Date: Re: segmentation fault sometimes
- Previous by thread: Re: A couple of things from H&S
- Next by thread: Re: A couple of things from H&S
- Index(es):
Relevant Pages
|
Loading