Re: A question on string literals



>> Chris Torek wrote:
>>> &foo - object context
>>> sizeof foo - object (or maybe even "sizeof") context
[snippage]
>>> and so on.

>Joe Wright <jwright@xxxxxxxxxxx> writes:
>> I know you Chris and I love you like a brother but given..
>> int foo = 2, bar = 3;
>>
>> &foo - is the address of foo, a 'value' of type (int*)
>> sizeof foo - is a value (4 at my house) of type (size_t)
>> ++foo - is a value (now 3) of type (int)
>>
>> None of these have 'object' context as I see it. What are you trying
>> to tell us here?

In article <ln64u5p4hp.fsf@xxxxxxxxxxxxxxx>
Keith Thompson <kst-u@xxxxxxx> wrote:
>In "&foo", he's talking about the context in which the name "foo"
>appears, not the entire expression. The argument to the unary "&"
>operator must be an lvalue, so foo is in an "object context". Similarly,
>the argument of unary "++" must be an lvalue.

Keith is right about this.

>He's mistaken about sizeof, which doesn't require an lvalue.

This is true, and is why I said "or maybe even `sizeof context'".
Sizeof is even more exceptional than unary-&.

Finally, I would like to note that -- while it was never added to
C99 -- there have been various proposals, over the years, to
implement array assignment in C. I suspect that C1X or C2X (if
these ever come about) may eventually do so -- and if so, I believe
the "most natural" way to handle array assignment is to add a rule
that says, in:

a = b

if "a" has type "array N of T", b is converted to a value of type
"pointer to T" (and if it does not match a diagnostic is required);
then the code acts much like a call of the form:

memcpy(&a[0], b, sizeof a)

except that the type of the result is "pointer to T" (rather than
"pointer to void").

If this *were* added to C, the left side of a simple asignment
operator would join the ranks of the "exceptions" for the
array-to-pointer conversion rule.

Of course, there are also "obvious" and "natural" (to me anyway)
interpretations for:

arr += arithmetic_type;
arr -= arithmetic_type;
arr++; /* same as arr += 1 */
arr--; /* same as arr -= 1 */
/* and so on for all arithmetic and logical operators */

which involve replicated arithmetic applying the right-hand-side
value (or implied 1, for ++ and --) to each element of the array,
with the value of the expression as a whole again being "pointer
to first element of array". (Some might object that this renders
identical values for arr++ and ++arr, to which I say: "so what?" :-) )

All of these interpretations arise from one single central idea:
naming an array always just names the entire array, but the "value"
of an array (when a value is needed) is computed by finding a
pointer to its first element. If a value is *not* needed, the
array still names the entire array.

Again, this is not quite what the actual C standards (C89 and C99
both) say, but it delivers the same result, in what I think is a
simpler, yet ultimately more powerful, way.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.



Relevant Pages

  • 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)
  • Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned?
    ... Using SizeOf on variables does not prevent programming mistakes. ... How will you solve the problem for pointer variables? ... For that, I use dynamic arrays, which case I use Length in conjunction with SizeOf to determine the amount of memory occupied by the array data. ...
    (alt.comp.lang.borland-delphi)
  • Re: De-referencing pointer to function-pointer
    ... >> Either your compiler is broken or you are not invoking it as a C ... >> is a constraint violation to apply the sizeof operator to a function ... >> pointer to the element type of the array. ... >> bytes, of the function, not of a pointer to the function. ...
    (comp.lang.c)
  • Re: Examples Heap Sort - problem
    ... |> inline std::size_t SizeOf(DataType& Source) ... | error that you ask the array length of a pointer. ... Chris Val ...
    (alt.comp.lang.learn.c-cpp)