Mail Archives: djgpp/1998/01/23/15:40:58
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; e<X; e++)
>menu[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
- Raw text -