init. cmplx struct in a simple way 
Author Message
 init. cmplx struct in a simple way

/*
Dear All,

I need to make an initialization of complex data structure
but it seems that C syntax is weak for this case.
Preprocessor doesn't help much as well. Here is my example:

*/

typedef struct ctrl_dependence_tag {
  unsigned host_id;                
  unsigned num_of_dependants;      
  unsigned *pDependant;            

Quote:
} Ctrl_Dependence;                  

/*
 To initialize this structure I use the following:
*/
unsigned list[] = {201,202,203};
Ctrl_Dependence my_ctrl_dep = {
  200,
  sizeof(list)/sizeof(list[0]),
  list

Quote:
}

/*
which is quite boring to type every time. In addition I use
the variable 'list' only ones here to make a reference to it
in 'my_ctrl_dep'.

So, Ideally I would like to
1. get read of name list (I dint want to invent names every
time)
2. make the above procedure with a simple macros call like:
*/

DECLARE_CTRL_DEPENDENCE(my_ctrl_dep, 200, {201,202,203})

/*
The problem is that I can not (i don't know how to) write
such a macro.

I tried and met at least to problems:
1. it seems that {201,202,203} is not allowed as macro
parameter
2. it is not allowed to make such tricks:
*/

Ctrl_Dependence my_ctrl_dep = {
  200,
  sizeof({201,202,203})/sizeof(unsigned),
  {201,202,203}

Quote:
}

/*
3. I tried then to follow the way with declaration of 'list'
but to generate a variable name on the fly using something
like
list##__LINE__ inside macro definition. But it didn't work
as well.

I would like to ask C wizards around here, what can I do in
this situation? Any suggestions? Any solutions?

Thanx in advance,
Edward

*/



Sat, 02 Nov 2002 03:00:00 GMT  
 init. cmplx struct in a simple way
Edward Goldobin a crit dans le message

Quote:
>/*
>Dear All,

>I need to make an initialization of complex data structure
>but it seems that C syntax is weak for this case.
>Preprocessor doesn't help much as well. Here is my example:

>*/

>typedef struct ctrl_dependence_tag {
>  unsigned host_id;
>  unsigned num_of_dependants;
>  unsigned *pDependant;
>} Ctrl_Dependence;

>/*
> To initialize this structure I use the following:
>*/
>unsigned list[] = {201,202,203};
>Ctrl_Dependence my_ctrl_dep = {
>  200,
>  sizeof(list)/sizeof(list[0]),
>  list
>}

>/*
>which is quite boring to type every time. In addition I use
>the variable 'list' only ones here to make a reference to it
>in 'my_ctrl_dep'.

>So, Ideally I would like to
>1. get read of name list (I dint want to invent names every
>time)
>2. make the above procedure with a simple macros call like:
>*/

>DECLARE_CTRL_DEPENDENCE(my_ctrl_dep, 200, {201,202,203})

>/*
>The problem is that I can not (i don't know how to) write
>such a macro.

>I tried and met at least to problems:
>1. it seems that {201,202,203} is not allowed as macro
>parameter
>2. it is not allowed to make such tricks:
>*/

>Ctrl_Dependence my_ctrl_dep = {
>  200,
>  sizeof({201,202,203})/sizeof(unsigned),
>  {201,202,203}
>}

>/*
>3. I tried then to follow the way with declaration of 'list'
>but to generate a variable name on the fly using something
>like
>list##__LINE__ inside macro definition. But it didn't work
>as well.

>I would like to ask C wizards around here, what can I do in
>this situation? Any suggestions? Any solutions?

>Thanx in advance,
>Edward

>*/

Try this one:
#define DECLARE_CTRL_DEPENDENCE(a,b,c,d,e)\
unsigned a##_list[] = {(c), (d), (e)};    \
Ctrl_Dependence (a) =                     \
{                                         \
   (b),                                   \
   sizeof a##_list / sizeof *a##_list,    \
   a##_list                               \

Quote:
}

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Show us your source code, and let us, like
rapacious vultures half-crazed by starvation, rip it to shreds until
there's practically nothing left" --Richard Heathfield CLC


Sat, 02 Nov 2002 03:00:00 GMT  
 init. cmplx struct in a simple way


Quote:
>I [want] to make an initialization of complex data structure

 [with a variable number of "unsigned int"s, without writing up
 names for arrays for each initializer.  This works:]

Quote:

>typedef struct ctrl_dependence_tag {
>  unsigned host_id;                
>  unsigned num_of_dependants;      
>  unsigned *pDependant;            
>} Ctrl_Dependence;                  

>unsigned list[] = {201,202,203};
>Ctrl_Dependence my_ctrl_dep = {
>  200,
>  sizeof(list)/sizeof(list[0]),
>  list
>}

 [but I want to automate it so that I can write something more like]

Quote:
>DECLARE_CTRL_DEPENDENCE(my_ctrl_dep, 200, {201,202,203})

I think we can prove that this is impossible as stated.

The problem is that the length of the array ("list" in the example
above) is variable.  If it were fixed -- always three, for instance
-- we could:

        #define DECLARE_CTRL_DEPENDENCE(a, b, c, d, e) /* ... something */

where "a" would be the name "my_ctrl_dep", "b" would be 200, and the
three identifiers "c", "d", and "e" would bind the tokens "{ 201",
"202", and "203 }" respectively.  But if it were fixed, the sizeof()
would be unnecessary, so obviously the actual number of brace-enclosed,
comma-separated items is variable.  Alas, the only way to
protect a comma inside an argument to a macro is to enclose it in
quotes (single or double) or parentheses.  That is:

        #define FOO(x, y)

        FOO("1, 2", (a, b))

passes two arguments to FOO(), but:

        FOO({1, 2}, {a, b})

passes four, giving an error.

Now, there is a way around this.  We can #define something that expands
to "," *after* the macro arguments are bound up:

        #define x ,
        FOO({1 x 2}, {a x b})

This does the trick, at the cost of breaking the specification slightly.

Having done that:

Quote:
>I tried and met at least to problems:
>1. it seems that {201,202,203} is not allowed as macro parameter

(the "#define x" trick handles this one)

Quote:
>2. it is not allowed to make such tricks:
>Ctrl_Dependence my_ctrl_dep = {
>  200,
>  sizeof({201,202,203})/sizeof(unsigned),
>  {201,202,203}
>}

Right -- the only anonymous array C offers is the string literal,
which will not do in this case.

Quote:
>3. I tried then to follow the way with declaration of 'list'
>but to generate a variable name on the fly using something like
>list##__LINE__ inside macro definition. But it didn't work
>as well.

This part is tricky, as you must use multiple levels of macro
expansion to get __LINE__ to expand to the current source line
number before the token "__LINE__" itself is concatenated with
the token "list".

        #define I1(x, y) x ## y
        #define I2(x, y) I1(x, y)
        #define UNIQNAME(name) I2(name, __LINE__)
        #define DECLARE_CTRL_DEPENDENCE(name, num, bracelist) \
        /*static*/ unsigned int UNIQNAME(list) = bracelist; \
        Ctrl_Dependence name = { \
                num, \
                sizeof UNIQNAME(list) / sizeof(unsigned int),
                UNIQNAME(list) \
        }

        #define x ,
        DECLARE_CTRL_DEPENDENCE(my_ctrl_dep, 200, {201 x 202 x 203})
        DECLARE_CTRL_DEPENDENCE(dep3, 300, {301 x 302 x 303 x 304 x 305})
        DECLARE_CTRL_DEPENDENCE(dep9, 900, {901 x 902})
        #undef x

Note that this trick will fail if you put two DECLARE_CTRL_DEPENDENCE
invocations on the same source line.

It is probably better to write out the declarations manually, or
write a program to generate a C-language file that contains the
declarations.  (This C "source" file will actually be object code,
and should be treated as such in your source code control system.
The actual source is the generating program and its input data
file.) you do the latter, and use the (commented-out) "static"
shown above, the generated "unique names" will not escape that
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sat, 02 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Simple ways to Improve my UI

2. Init a struct with its own size

3. init of recurrent struct decl

4. Partial init of automatic struct vars?

5. Functions passed in structs, as args, and init'd

6. struct init problem

7. init. complex structues in a simple way

8. cmplx.h

9. Simple struct question, part three

10. Simple struct question, part deux

11. Simple struct question

12. simple switch case & struct program, screwy results

 

 
Powered by phpBB® Forum Software