[comp.lang.c] type casting

peter@thirdi.UUCP (Peter Rowell) (04/28/88)

I have two questions regarding type casting.  Both of them arise from
the need to improve the appearance of some code, probably using
#define's.  It is important that the code compile on a *very* wide
assortment of machines/compilers with no compile errors and no
lint errors.

1)	Is type casting of the left hand side of an expression supported
	in ANSI C?  Any place other than BSD?
	E.g.  int *pfoo; (char *)pfoo = malloc(10*sizeof(pfoo[0]));

2)	Is it possible to use a variable in such a way as to imply its
	type for casting purposes?
	E.g.  int *pfoo; pfoo = (typeof(pfoo)) malloc(10*sizeof(pfoo[0]));


We would like to do something like:
	#define AdjustArray(arr,cnt) \
		((char *)arr = realloc((char *)arr, cnt * sizeof(arr[0])))
OR
	#define AdjustArray(arr,cnt) \
		(arr = (typeof(arr)) realloc((char *)arr, cnt * sizeof(arr[0])))

and then replace
	pfoo = (int *) realloc((char *)pfoo, new_size * sizeof(pfoo[0]));
with
	AdjustArray(pfoo, new_size);


Replies by mail, please.

	Peter Rowell
	Third Eye Software, Inc.
	(415) 321-0967

chris@mimsy.UUCP (Chris Torek) (04/28/88)

In article <400@thirdi.UUCP> peter@thirdi.UUCP (Peter Rowell) writes:
>1)	Is type casting of the left hand side of an expression supported
>	in ANSI C?  Any place other than BSD?

It is not even supported in 4BSD now.

	"foo.c", line 4: illegal lvalue operand of assignment operator
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris