Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!lll-tis!ames!hc!hi!kurt From: kurt@hi.unm.edu (Kurt Zeilenga) Newsgroups: comp.lang.c Subject: Re: Allocating a two-dimensional array Message-ID: <22941@hi.unm.edu> Date: 17 Jan 88 21:12:31 GMT References: <1050@ogg.cgrg.ohio-state.edu> Reply-To: kurt@hi.unm.edu (Kurt Zeilenga) Organization: U. of New Mexico, Albuquerque Lines: 24 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: #define malloc_matrix(p,n,m,t) ((p) = ((t) *) malloc((n)*(m)*sizeof(t))) #define matrix(p,n,m,x,y) ((p)[(n)*(x)+(y)]) where t = element type p = pointer to type n,m = dimensions x,y = indexes malloc_matrix() allocates the space and matrix() is used to pick the element. There are many similiar macros that will do the same thing, these ones may be an overkill. -- Kurt (zeilenga@hc.dspo.gov)