Microsoft 'C' - Strange behaviour with doubles
Chris Torek
chris at umcp-cs.UUCP
Tue May 6 01:10:22 AEST 1986
In article <200 at pyuxv.UUCP> cim2 at 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 at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list