Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c,net.micro.pc,net.unix Subject: Re: Microsoft 'C' - Strange behaviour with doubles Message-ID: <1314@umcp-cs.UUCP> Date: Mon, 5-May-86 11:10:22 EDT Article-I.D.: umcp-cs.1314 Posted: Mon May 5 11:10:22 1986 Date-Received: Thu, 8-May-86 07:07:31 EDT References: <200@pyuxv.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 62 Xref: linus net.lang.c:8161 net.micro.pc:7715 net.unix:7132 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