libs and constants

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SerGioGio

    #1

    libs and constants

    Hello,

    I want to build a ".lib" library where I will have some constants.
    The constants are not only int, char, but also struct constants.
    How do I declare / implement these struct constants? I came up with 3
    solutions but I am really satisfied with none of them:

    1. use #define
    This can work with constants int or char*, but not really with structures.
    If you have
    | #define my_struct_const ant { { 1, 2 }, 3 }

    you can then write
    | my_struct constant = my_struct_const ant;
    but subsequentely you cannot write
    | constant = my_struct_const ant;

    2. declare the variable in .h:
    | extern my_struct my_struct_const ant;
    and implement in a .c:
    | my_struct my_struct_const ant = { { 1, 2 }, 3 };
    you can then write
    | my_struct constant = my_struct_const ant;
    | constant = my_struct_const ant;

    So far so good.
    But I understand however that the initialisation will not be performed...
    So I will need to provide an init() function and require the users of the
    ..lib to call these function...
    In addition I will need two lines in two separate files (declaration /
    implementation) so it is harder to maintain.

    3. provide a make function
    | extern my_struct make_my_struct( int, int, int);
    and use #define:
    | #define my_struct_const ant make_my_struct( 1, 2, 3)
    you can then write
    | my_struct constant = my_struct_const ant;
    | constant = my_struct_const ant;
    But this does not scale very well, you need to provide make functions for
    all the types you will have constants.

    Are there any better options than thses ones?

    Thanks in advance for your comments and suggestions!

    SerGioGioGio


  • Chris Torek

    #2
    Re: libs and constants

    In article <4295d2ba$1@new s.starhub.net.s g> SerGioGio <nospam> wrote:[color=blue]
    >I want to build a ".lib" library where I will have some constants.
    >The constants are not only int, char, but also struct constants.[/color]

    Only C99 has actual support for structure-valued constants.

    If all of your compilers support (at least this much) C99, you
    can write, e.g.:

    #define ZORG ((const struct evil) {{ 1, 2 }, 3 })

    (The "const" is not required, but I consider it a good idea.)

    Note that the user of this constant must take care to avoid
    taking the address of the constant, which does work but may
    point to an object with automatic storage duration:

    #ifdef OLDWAY
    typedef double evil_t;
    #define ZORG (42.0)
    #else
    typedef struct evil evil_t;
    struct evil { int b[2]; int c; };
    #define ZORG ((const struct evil) {{ 1, 2 }, 3 })
    #endif

    const evil_t *foo(void) {
    const evil_t *p;
    ...
    p = &ZORG;
    return p; /* ERROR */
    }

    One cannot take addresses of, e.g., simple int- and double-valued
    constants, so programmers are not particularly *likely* to attempt to
    take the address of the constant. If one defines OLDWAY above, the
    code fails to compile.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.

    Comment

    Working...