Re: A couple of things from H&S



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"
.



Relevant Pages

  • Re: Null terminated strings: bad or good?
    ... The sizeof operator yields the size of its operand, ... If the type of the operand is a variable length array type, ... As far as I can tell, the standard doesn't explicitly forbid naming types larger than SIZE_MAX bytes, or even applying sizeof to such types -- it just describes the semantics of sizeof in a way that is logically impossible for such types. ... What this means, in effect, is that it's actually the size of its operand that is implementation-defined; the fact that the value is implementation-defined does not give the implementation the option of having sizeof yield any value other than "the size of it's operand". ...
    (comp.std.c)
  • Re: Wording glitch: sizeof array vs. sizeof (array)
    ... so it's implicitly converted to a pointer. ... That type is an array type, ... the unparenthesized expression "array" is subject to the conversion, ... because it's not an operand of sizeof. ...
    (comp.std.c)
  • Re: function pointers as function parameters
    ... > doesn't "decay" to a pointer to the function, ... Because this conversion does not occur, the operand of the sizeof ... only to make it a constraint violation ...
    (comp.lang.c)
  • Re: [C or C++] Is this legal? sizeof *p
    ... where the size of an automatic array is defined at run time. ... > that case if sizeof is applied to a VLA, ... But applying sizeof to a VLA is applying ... > sizeof to an array, not a pointer, and using the name of an array as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How can I get the size of this?
    ... > I am now having a problem at using sizeof() function ... the pointer. ... Operating on something declared as an array returns the size of the ... LPCSTR is already typedef'd as "const char *", ...
    (microsoft.public.vc.language)

Loading