Indefinite-length array as member of struct: how?

Chris Torek chris at mimsy.UUCP
Thu Jul 13 07:01:16 AEST 1989


(I should change the title, but cannot think of a better one.)

>In article <18492 at mimsy.UUCP> I wrote:
>>	char string[SIZE]; ... *string = <any integer expression>;
>>is legal ...

In article <2948 at nmtsun.nmt.edu> dwho at nmtsun.nmt.edu (David Olix) writes:
>I am not one to be picky (usually), but isn't *string (a.k.a. string[0])
>of type char?  If so, any expression yeilding a result of type char
>would be legal.

It would, except that no expression yeilds a result of type `char'
by the time one gets around to doing assignment, because the value
promotes to one of type `int'.  (The exact duration of the `char'
type really depends on your compiler.  The longer the compiler
retains the type, the more likely it is to generate decent code.
But in principle, `char c, d; c = d;' means `fetch d, widen to int,
narrow to char, store in c'.)

>Yes, an integer expression would also be legal, but depending on the
>result of the expression and the sizes of char and int on the particular
>machine you could end up losing bits.

As long as we are being picky: `integer expression' includes values
of type `long'.  At any rate, the situation is possibly worse: code
such as

	char c; c = 12345;

is allowed (by the pANS) to produce weird results such as having the
display leap off your desk, run in circles, then turn into a butterfly.
The value of that integer-expression must fit in an object of type
`char', or the result of the assignment is {un or implementation}-
defined.  (I cannot recall which offhand.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list