libs and constants

Discussion in 'C Programming' started by SerGioGio, May 26, 2005.

  1. SerGioGio

    SerGioGio Guest

    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_constant { { 1, 2 }, 3 }

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

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

    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_constant make_my_struct(1, 2, 3)
    you can then write
    | my_struct constant = my_struct_constant;
    | constant = my_struct_constant;
    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
     
    SerGioGio, May 26, 2005
    #1
    1. Advertising

  2. SerGioGio

    Chris Torek Guest

    In article <4295d2ba$> SerGioGio <nospam> wrote:
    >I want to build a ".lib" library where I will have some constants.
    >The constants are not only int, char, but also struct constants.


    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.
     
    Chris Torek, May 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Drazen Gemic
    Replies:
    5
    Views:
    5,440
    Drazen Gemic
    Oct 22, 2005
  2. Brian
    Replies:
    0
    Views:
    337
    Brian
    Dec 19, 2003
  3. Christoph
    Replies:
    2
    Views:
    549
    Richard Bos
    Sep 17, 2003
  4. Raman
    Replies:
    5
    Views:
    1,081
    Raman
    May 9, 2008
  5. Greg Hauptmann
    Replies:
    4
    Views:
    218
    Stefano Crocco
    Feb 7, 2009
Loading...

Share This Page