TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Simon Avery
from: Darin McBride
date: 1998-08-07 09:06:18
subject: Dynamic Structs?

SA> But, how do I create a variable number of structs, allocating the
 SA> memory when needed.

malloc.  :-)

 SA> int num_of_configs=atoi(argv[1]);        // for example

 SA> // Mystery Code here please. :)

CONFIG* pConfigs = malloc(num_of_configs * sizeof(*pConfigs));

Remember to free(pConfigs) when you're done with them.  Anyway, you will
have pConfigs[0] through pConfigs[num_of_configs - 1] to use.  For example,
if you have a struct member 'foo', you could access the i'th one as:

pConfigs[i].foo

Hope this helps!


---
* Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 250/102 201 99 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.