Date: Fri, 23 Jan 1998 12:40:13 -0800 (PST) Message-Id: <199801232040.MAA27032@adit.ap.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" To: Jukka Mickelsson , djgpp AT delorie DOT com From: Nate Eldredge Subject: Re: How to use malloc? Precedence: bulk At 03:45 1/23/1998 +0200, Jukka Mickelsson wrote: >I have a problem using the malloc() function since I don't really know >what to do with it. >I have the following type program: > >struct menuitem() { // this source code is simplified No parenthesis here. Just `struct menuitem { ... }' > char *description[25]; >} Add a semicolon. > >struct menuitem menu[5]; > >for(i=0; i<5; i++) >for(e=0; emenu[i].description[e] = "testing"; Okay, > >The problem is that when X > 3 the program starts to act in a weird way >or crashes >sometimes. This should work, but note that each string "testing" will be read-only. Actually, they will all be the same string. If you want a copy in each place, do something like menu[i].description[e] = strdup("testing"); >I probably should use malloc() here but I haven't got a >slightest idea _where_ >and _how_. I don't see anywhere that needs mallocing. This is a standard C question and would be far better asked on comp.lang.c. Nate Eldredge eldredge AT ap DOT net