A cast pointer is NOT an lvalue!? (ANSI C)

Chris Torek chris at mimsy.UUCP
Thu Nov 10 05:22:11 AEST 1988


In article <42234 at yale-celray.yale.UUCP> wald-david at CS.YALE.EDU
(david wald) writes:
>How about:
>
>    value = *(long *)argp;
>    argp = (argtype *)((char *)argp + sizeof(long *));
>
>where argtype is the type of *argp?

You can write that, and it will probably compile, but it may not do
anything useful, and may even crash.  As a concession to weird
architectures, the dpANS grants license to store pointers to things of
varying sizes in cells of varying sizes.  In particular, if `argtype'
is, say, double, there may not be enough bits in it to retain a pointer
to a long, and code like

	while (argp < endp) {
		(void) printf("%ld\n", *(long *)argp);
		argp = (double *)((char *)argp + sizeof(long *));
	}

may loop forever printing the same value each time.

As a concession to getting work done, however, the dpANS grants license
to use something similar to varargs to do the job.  So why not use it
instead of trying to come up with an alternative?
-- 
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