Path: utzoo!utgpu!water!watmath!clyde!rutgers!sdcsvax!ucsdhub!esosun!seismo!uunet!mcvax!nikhefh!n62 From: n62@nikhefh.hep.nl (Klamer Schutte) Newsgroups: comp.lang.c Subject: Allocating two dimensional arrays Message-ID: <419@nikhefh.hep.nl> Date: 20 Jan 88 12:06:25 GMT Reply-To: n62@nikhefh.hep.nl (Klamer Schutte) Organization: Nikhef-H, Amsterdam (the Netherlands). Lines: 28 >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. and as a proposed solution: >#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)]) Why not simple something like this: struct matrix { float data[n][m]; } *foo; ... foo = ( struct matrix * )malloc( sizeof( struct matrix )); and accessing the data like: foo->data[n][m]; In this way the source code will reflect ecxactly how the data is accessed; I think C programming should be done that way.