Re: How do I do generic programming in C?

Discussion in 'C Programming' started by swengineer001@gmail.com, May 13, 2008.

  1. Guest

    >
    > So... is there any way of emulating templates in C?
    > Or do I just have to live with duplicate functions?
    >
    > --
    > Cheers,
    > Robbie Hatley
    > lonewolf aatt well dott com
    > www dott well dott com slant user slant lonewolf slant


    Why not use a structure containing a type value and a union of the 2
    structures that can be passed:

    typedef enum
    {
    firststructure = 1,
    secondstructure = 2
    } MyStructures;

    typedef struct
    {
    ..........
    } structure1;
    typedef struct
    {
    ........
    } structure2;

    typedef union
    {
    structure1 first;
    structure2 second;
    } MyUnion;

    typedef struct
    {
    MyStructures structur_type;
    MyUnion TheStructure;
    } TheArgument;

    Obviously you would want to name everything appropriately.
    , May 13, 2008
    #1
    1. Advertising

  2. wrote:
    > >
    > > So... is there any way of emulating templates in C?
    > > Or do I just have to live with duplicate functions?

    >
    > Why not use a structure containing a type value and a union
    > of the 2 structures that can be passed:

    <snip>
    > typedef union
    > {
    > structure1 first;
    > structure2 second;
    > } MyUnion;
    >
    > typedef struct
    > {
    > MyStructures structur_type;
    > MyUnion TheStructure;
    > } TheArgument;


    Two seconds thought should tell you why this is not
    a good idea for converting one struct type to another.

    --
    Peter
    Peter Nilsson, May 13, 2008
    #2
    1. Advertising

  3. Guest

    Dummy way:

    int Setup1400 (setup1400_t const *exp_in, setup_t *exp_out, setup_t
    const *ext_in, setup1400_t *ext_out)
    {

    if (exp_in)
    do ext_in related stuff
    else
    do ext_in related stuff

    if (!in || !out) return 666;

    memset(out, 0, sizeof(*out));

    out->CoolStages = in->CoolStages;
    out->HeatStages = in->HeatStages;
    out->StageWidth = in->StageWidth;
    out->TimePeriods = in->TimePeriods;
    (several hundred more lines of "out->foo = in->foo;")

    return 42;

    } // end ExpandSetupFrom1400()
    , May 14, 2008
    #3
  4. "Robbie Hatley" <> writes:
    [...]
    > Let's see...
    >
    > #include <stdio.h>
    >
    > typedef struct Zamboni_tag
    > {
    > int fred;
    > double trouble;
    > } Zamboni;
    >
    > int main (void)
    > {
    > Zamboni OfMyOwni;
    > void* Ewww;
    >
    > Ewww = &OfMyOwni;
    > ((Zamboni*)Ewww)->fred = 7;
    > printf ("OfMyOwni.fred = %d\n", OfMyOwni.fred);
    > return 0;
    > }
    >
    > Nah, that'll never compile. Let me try....
    >
    > HOLY SHIT, that compiles and runs! I didn't think you could
    > actually use a typecast as an LValue like that.

    [...]

    You can't, and you didn't. The left operand of "->" isn't required to
    be an lvalue.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2008
    #4
    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. Hung Jung Lu
    Replies:
    6
    Views:
    670
    Neal Gafter
    Nov 18, 2003
  2. Paul O'Grady
    Replies:
    19
    Views:
    700
    Patricia Shanahan
    Feb 18, 2005
  3. Murat Tasan
    Replies:
    1
    Views:
    8,025
    Chaitanya
    Feb 3, 2009
  4. Replies:
    2
    Views:
    423
  5. minlearn
    Replies:
    2
    Views:
    445
    red floyd
    Mar 13, 2009
Loading...

Share This Page