Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!sdd.hp.com!mips!dimacs.rutgers.edu!rutgers!cbmvax!cbmehq!cbmger!kbsaar!fjrei From: fjrei@kbsaar.UUCP (Franz-Josef Reichert) Newsgroups: comp.sys.amiga.programmer Subject: Re: Dynamic struct's Message-ID: Date: 9 May 91 21:13:51 GMT References: <52955@nigel.ee.udel.edu> Organization: Private Amiga Site Lines: 48 In article <52955@nigel.ee.udel.edu> DA3721A@acad.drake.edu writes: >I am currently working on programming a fairly simple database in C. The >program will allow the user to specify the number of fields in the database >and their names. My question is: How does one dynamically declare a struct >for accessing the correct number of fields? In other words, rather than doing > > struct { > int field1; > char field2; > int field3; > } record; > >I would like to do something like > > struct { > int field1; > char field2; > . > . > . > int fieldx; > } record; > >where 'x' in 'fieldx' is the number of fields as specified by the user. Note >that the types in the struct are not important here, they're just for the >example. There's no way doing 'dynamically' structure declaration in C. All you can do is to declare a structure member which points to dynamically allocatable data, e.g. struct { int field1; int *fields; } record; So you have to allocate space for 'x' int's (x*sizeof(int)) in your program and store a pointer to this array in record.fields. The elements of this array can be referenced via record.fields[i], where 'i' is an index ranging from 0 to x-1. > Thanks in advance, > David Aschbrenner -- Best regards, Franz-Josef Reichert VOICE: +49 6805 7417 Kuchlingerstrasse 13 UUCP: ...uunet!cbmvax!cbmehq!cbmger!kbsaar!fjrei D-6601 Kleinblittersdorf GERMANY