[net.unix] Microsoft 'C' - Strange behaviour with doubles

cim2@pyuxv.UUCP (Robert L. Fair) (05/04/86)

Consider an array of 15 pointers to arrays of doubles:

	double (*parray[15])[];

The following code to 'malloc' the actual double array barfs on Microsoft 'C',
with a 'different levels of indirection' warning on the '='.
The code passes through LINT, and compiles OK on UN*X 5.2

	char	*malloc();

	parray[0] = (double*)malloc((unsigned)sizeof(double)*75);

Microsoft produces the same error if the coercion is (double**), (double),
or nothing at all !

Any ideas ?

Rob. Fair
{ihnp4|gamma|bambi}!pyuxv!cim2

Bell Communications Research
Piscataway
New Jersey

Subject: 
Newsgroups: general

chris@umcp-cs.UUCP (Chris Torek) (05/05/86)

In article <200@pyuxv.UUCP> cim2@pyuxv.UUCP (Robert L. Fair) writes:

[concerning Microsoft C's warning on the assignment below]

>	double (*parray[15])[];
>	char	*malloc();
>
>	parray[0] = (double*)malloc((unsigned)sizeof(double)*75);

`parray' here is `array 15 of pointer to array of double'.
Indirection (parray[0]) yields `pointer to array of double';
so the proper cast is

	parray[0] = (double (*)[]) malloc(...);

but as cdecl says,

	Warning: Unsupported in C -- Pointer to array of unspecified
	dimension

Most likely your intent here is to create a fifteen element vector
of vectors, where the subsidiary vectors contain an unspecified
number of objects of type `double'.  (I am trying to avoid the
words `array' and `pointer', if it is not obvious.)  To accomplish
this, try the following:

	double *vecvec[15];

	vecvec[0] = (double *) malloc((unsigned) (j * sizeof (double)));

You can then reference vecvec[0][0 .. j-1] (if you will pardon the
Pascalesque notation), or *(vecvec[0]) through *(vecvec[0] + j - 1),
if you prefer.

More generally, given

	int i;			/* loop index */
	int *j;			/* bounds on each vec[i] */
	int n;			/* bound on vec[] */
	double **vecvec;	/* vector of vectors of doubles */

	/* create a vector of n objects of type t */
#define MAKEVEC(n, t) ((t *) malloc((unsigned) ((n) * sizeof (t))))

	n = ...;
	j = MAKEVEC(n, int);
	for (i = 0; i < n; i++)
		j[i] = ...;
	vecvec = MAKEVEC(n, double *);
	for (i = 0; i < n; i++)
		vecvec[i] = MAKEVEC(j[i], double);

you can then reference vecvec[i][0 .. j[i] - 1] for i in [0, n).

Of course, all of the above needs to ensure that malloc() succeeds;
if you leave out such tests, your program will work perfectly until
the first time you demonstrate it to someone important, at which time
it will bomb spectacularly.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu