Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ginosko!uunet!telxon!gorpong From: gorpong@telxon.uucp (Gordon C. Galligher) Newsgroups: comp.lang.c Subject: Re: Question: malloc on 2-d array Summary: Try calloc() or malloc(dim * sizeof(...)) Keywords: malloc, 2-d array Message-ID: <70@telxon.UUCP> Date: 11 Jul 89 16:23:30 GMT References: <15372@eecae.UUCP> Sender: news@telxon.UUCP Reply-To: gorpong@telxon.UUCP (Gordon C. Galligher) Distribution: usa Organization: Telxon Corporation, Akron Ohio (NNTP with a :-) Lines: 33 In article <15372@eecae.UUCP> pantz@brain.mth.msu.edu writes: > >Does anyone know how I can allocate memory space for a multi-dimensional >array? Thanks in advance. (In the following examples, you can substiture your favorite pointer in place of the char *'s): On some systems the calloc() call is allowed. You declare a variable: char **a, *calloc(); if ( (a = (char **) calloc(25, sizeof(char *))) == NULL ) error_message_here; This will allocate at least enough memory to be used like an array of 25 elements, each element being a pointer to a character. To put strings INTO these places, then you need to call malloc() for each a[x] you want to store. If calloc() doesn't exist, then you can coerce malloc() into doing it for you: char **a, *malloc(); if ( (a = (char **) malloc((25 * sizeof(char *)))) == NULL ) error_message_here; Again, the malloc() for each a[x] is needed. -- Gordon. Gordon C. Galligher <|> ...!uunet!telxon!gorpong <|> gorpong@telxon.uucp.uu.net Telxon Corporation <|> "Admit it, ve're lost!" - Checkov "Alright, we're Akron, Ohio, 44313 <|> lost, but we're making great time." - Sulu (216) 867-3700 (3512)<|> Star Trek V: The Final Frontier