Help me cast this!

Chris Torek chris at mimsy.UUCP
Wed May 4 00:43:17 AEST 1988


In article <294 at fedeva.UUCP> wrd3156 at fedeva.UUCP (Bill Daniels) writes:
>	char *malloc();
>	struct outfile {
>		int j;
>		int k;
>	} (*output)[];
>	output = malloc(sizeof(struct outfile) * 3);

First, note that output is not a proper variable:

	% echo 'declare output as pointer to array of struct outfile' | cdecl
	Warning: Unsupported in C -- Pointer to array of unspecified dimension
	struct outfile (*output)[]
	% 

In particular, your compiler will let you write (*output)[i], or
output[0][i], but try output[j][i] and <boom>!  I have argued this
point here before; I will only say now that this is not what C wants
you to say to create a dynamic array 3 of struct outfile: you should
be using a simple pointer.  Nonetheless, it happens to work.

On to the cast:  The syntax of a cast is exactly the same as
the syntax of a declaration, minus the variable name.  Hence to
cast malloc to the same (unsupported) type:

	output = (struct outfile (*)[])malloc(...);
-- 
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