DA3721A@acad.drake.edu (05/08/91)
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. Can anybody help me out on this one? I hope I explained things clearly :) Thanks in advance, David Aschbrenner
dillon@overload.Berkeley.CA.US (Matthew Dillon) (05/09/91)
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; I generally do the following: struct { HeadInfo rc_Info; int rc_fields[XX]; } record; Where XX can be 0, 1, or MAXFIELDS depending on how you implement the structure. For example, if you use MAXFIELDS then you would allocate the structure like this: #include <stddef.h> struct record *rec = malloc(offsetof(struct record, rc_fields) + sizeof(rec->rc_fields[0]) * NumFields); If you use XX = 0 (int rc_fields[0]), assuming the compiler allows it, you can allocate the structure like this: struct record *rec = malloc(sizeof(struct record) + sizeof(rec->rc_fields[0]) * NumFields); If you use XX = 1 (int rc_fields[1]), which the compiler had better allow, you can allocate the structure like this: struct record *rec = malloc(sizeof(struct record) + sizeof(rec->rc_fields[0]) * (NumFields - 1)); Once allocated, you can initialize/use up to NumFields of the array rc_field. I included the HeadInfo type just to show you how to allocate nominal structures with extensions. > > Thanks in advance, > David Aschbrenner -Matt -- Matthew Dillon dillon@Overload.Berkeley.CA.US 891 Regal Rd. uunet.uu.net!overload!dillon Berkeley, Ca. 94708 USA
fjrei@kbsaar.UUCP (Franz-Josef Reichert) (05/10/91)
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
comeau@ditka.Chicago.COM (Greg Comeau) (05/10/91)
In article <52955@nigel.ee.udel.edu> DA3721A@acad.drake.edu writes: >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; There is no direct language support for this in C. However alternates can range from your writing out C code with the new structure, compiling it, and access routines to it, and throwing them into a dynamic/share library, to building the structure fully in a dynamic manne with things like malloc() and an array of structures (continaing info like: field name, type of field, field length, field alignment requirements, etc). Neither is really an attractive choice. - Greg -- Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418 Producers of Comeau C++ 2.1 Here:attmail.com!csanta!comeau / BIX:comeau / CIS:72331,3421 Voice:718-945-0009 / Fax:718-441-2310