Question:
How to fill a structure in C?
?
2011-07-20 16:06:54 UTC
I have a huge structure in C with around 200 elements that needs to be filled with some values (which I have in another text file). This is just direct hard coding for certain test cases.

As it happens, there are multiple instances of structure within structure etc etc in the main structure.
I have a text file in which all the values are listed in order.
I need to port these values to the respective elements in the structure.

Is there any tool which lists all the elements recursively inside a structure ?

For ex, if a structure A has elements B,C,D
And lets assume that
B is a structure with an integer say :- age.
C is a structure with 3 elements say :- circle, square, triangle.
D is a char

Is there a tool which will list all the elements in order like

A.B.age
A.C.circle
A.C.square
A.C.triangle
A.D

Or should i use some other approach ??
Three answers:
green meklar
2011-07-20 17:47:07 UTC
I don't think there is any native tool for doing something like that.



However, assuming that you're hardcoding the characteristics of the structure A, there's nothing stopping you from writing a fairly simple function that would carry out this task. In this case, since an instance of A doesn't contain other instances of A, nor do the structures B or C contain anything other than primitive member variables, a single function call would be sufficient. The more complex case would be if A could contain other instances of A, in which case you would have the function make a recursive call to itself and pass in the member variable. In this latter case, you would also have to either ensure that the program never set up a cycle in the references, or add something to the function to check for such cycles at runtime. Both approaches can be made to work.
husoski
2011-07-20 16:53:44 UTC
Here's an idea that I've used on multiple projects with complex structures. Create a set of macros that can be used as a sort of "language" for declaring your struct. As a very simple example.



DEFSTRUCT( TypeARecord, "Type A Equipment Record" )

FIELD(id, int, 0, "6-digit I.D. Number" )

FIELD(weight, double, 0.0, "Item Weight")

STRING(name, 30, "", "Item Name")

ENDSTRUCT( TypeARecord )



Not pretty, and more information that you need for a struct declaration, but there's a reason. Put these lines into a separate file to be included, and then, where needed, you can do something like:



#define DEFSTRUCT(Name, Desc) typedef struct Name {

#define FIELD(Name, Type, Init, Desc) Type Name;

#define STRING(Name, MaxLen, Init, Desc) char Name[(MaxLen)+1];

#define ENDSTRUCT(Name) } Name;



#include "A_def.inc"



#undef DEFSTRUCT

#undef FIELD

#undef STRING

#undef ENDSTRUCT



That will expand to the usual struct type definition, with a small convenience of adding 1 to string lengths to allow for a terminating '\0'. However, with a different set of macro definitions in a different place, probably in a different source file, you can have those macros expand to a braced initializer list, or a set of printf() statements to produce program documentation, or scanf() format strings or whatever the simple syntax of macro expansion will let you do. They're easier to parse than XML too, so you can do more sophisticated processing by feeding those to a program.



I've used this a lot for something related: Managing a large set of enum definitions. In one include it generates the plain enum definition. In another, it generates an array of strings for converting enum index values to printable text, and in yet another it generates an array of function pointers, so that each enum value can have a custom "handler".



I've also used that idea to use the same set of macros to generate online help text for an embedded product, and separately to generate text for a printable manual.



It's not all that pretty, but it does provide a single point of maintenance, kind of a "data dictionary" in code. It's nice NOT having to worry about partial updates. A project rebuild always has everything in sync.
sheldonlinker
2011-07-20 16:29:25 UTC
First, you're asking about getting data into the structure, then you show an output example. You can do input like this:



scanf("%d%s%s%s%c", &A.B.age, &A.C.circle, &A.C.square, &A.C.triangle, &A.D);



You can print like this:



printf("A.B.age=%d\nA.C.circle=%s\nA.C.square=%s\nA.C.triangle=%s\nA.D=%c\n", A.B.age, A.C.circle, A.C.square, A.C.triangle, A.D);



...or you can use PL/1, and then use GET DATA(A); and PUT DATA(A);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...