type casting
Peter Rowell
peter at thirdi.UUCP
Thu Apr 28 06:48:17 AEST 1988
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
More information about the Comp.lang.c
mailing list