Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ucbvax!husc6!cca!g-rh From: g-rh@cca.CCA.COM (Richard Harter) Newsgroups: comp.lang.c Subject: Re: Allocating a two-dimensional array Message-ID: <23344@cca.CCA.COM> Date: 17 Jan 88 23:18:24 GMT References: <1050@ogg.cgrg.ohio-state.edu> <22941@hi.unm.edu> Reply-To: g-rh@CCA.CCA.COM.UUCP (Richard Harter) Organization: Computer Corp. of America, Cambridge, MA Lines: 51 In article <22941@hi.unm.edu> kurt@hi.unm.edu (Kurt Zeilenga) writes: >Spencer@ogg.cgrg.ohio-state.edu (PROCEED) writes: >>Maybe this can't be done. Maybe I'm not thinking about it the right way. >>But I can't figure out how to dynamically allocate a two-dimensional >>array of floats. (I have to have an N by N matrix of floats, but don't >>know N beforehand.) Can anyone help me on this one? Thanks. > >You can not allocate multi-dimensional arrays dynamically. However, >you can do is something like: .... macros deleted .... I will defer the question of whether Kurt is correct or not to people more knowledgable in the arcana of C than I am. I don't know whether you can cast something as being a pointer to an N by N array where N is a variable. However a rather simple approach is to use an array of pointers, e.g. float **array; char *malloc(); /* No one forgets this declaration, right? */ int i; ..... array = (float **)malloc(N*sizeof(float *)); for (i=0;i