Specifying data types inside functions

Discussion in 'C Programming' started by No One, Apr 10, 2004.

  1. No One

    No One Guest

    Here is my problem:

    I have a certain set of well-defined manipulations that I have to apply
    to different types of data. In all cases the manipulations are exactly the
    same, and are to be performed on the different types of data. Currently I
    have a collection of functions that do exactly the same - the only
    difference between them is the type of data they act on. Let me present a
    toy example:

    I have the following two data types:

    typedef struct { typedef struct {
    int x ; int x ;
    int y ; char * p ;
    char * s ; } data_two ;
    } data_one ;

    Thus, I know that both data_one and data_two have an integer field, x.
    The remaining fields can be arbitrarily different.

    I want to have a function that takes as an argument a pointer to one of
    those structures, increments the x member and returns a pointer to the
    same structure. Currently I have two functions:

    data_one * f_one(data_one * data) data_two * f_two(data_two * data)
    { {
    data->x += 1 ; data->x += 1 ;

    return data ; return data ;
    } }

    The bodies of the functions are identical - which is a waste.
    I'd rather use a single function, with a prototype similar to:

    void * f(data_type type, void * data)

    where "type" would specify the actual type of the data we are dealing with.

    Can something along these lines be done? Please remember that the above
    is just a toy example.
     
    No One, Apr 10, 2004
    #1
    1. Advertising

  2. No One

    Malcolm Guest

    "No One" <> wrote in message
    > I have the following two data types:
    >
    > typedef struct { typedef struct {
    > int x ; int x ;
    > int y ; char * p ;
    > char * s ; } data_two ;
    > } data_one ;
    >
    > I'd rather use a single function, with a prototype similar to:
    >
    > void * f(data_type type, void * data)
    >
    > where "type" would specify the actual type of the data we are
    > dealing with.
    >

    You very rapidly get into a murky area of the C standard here. There are
    some guarantees about structure layout in memory, but it is generally not a
    good idea to use extendable structures or to try to convert one type of
    structure to another.

    If two structures have a common header then do this

    struct common
    {
    int x;
    ...
    };

    typedef struct
    {
    struct common header;
    ...
    }data_one;

    typedef struct
    {
    struct common header;
    ...
    } data_two;

    Now when you write f

    void f(data_type type, void *data)
    {
    data_one *data1;
    data_two *data2;
    switch(type)
    {
    case 1:
    data1 = data;
    foo( data1->header);
    break;
    case 2:
    data2 = data;
    foo(data2->header);
    break;
    }
    }

    Theoretically you could cast data to a struct common * directly, but this is
    asking for trouble. For instance, if someone inserts another member at the
    beginning of one of the structs the code will break without warning.
     
    Malcolm, Apr 10, 2004
    #2
    1. Advertising

  3. No One

    Peter Ammon Guest

    No One wrote:
    > Here is my problem:
    >
    > I have a certain set of well-defined manipulations that I have to apply
    > to different types of data. In all cases the manipulations are exactly the
    > same, and are to be performed on the different types of data. Currently I
    > have a collection of functions that do exactly the same - the only
    > difference between them is the type of data they act on. Let me present a
    > toy example:
    >
    > I have the following two data types:
    >
    > typedef struct { typedef struct {
    > int x ; int x ;
    > int y ; char * p ;
    > char * s ; } data_two ;
    > } data_one ;
    >
    > Thus, I know that both data_one and data_two have an integer field, x.
    > The remaining fields can be arbitrarily different.
    >
    > I want to have a function that takes as an argument a pointer to one of
    > those structures, increments the x member and returns a pointer to the
    > same structure. Currently I have two functions:
    >
    > data_one * f_one(data_one * data) data_two * f_two(data_two * data)
    > { {
    > data->x += 1 ; data->x += 1 ;
    >
    > return data ; return data ;
    > } }
    >
    > The bodies of the functions are identical - which is a waste.
    > I'd rather use a single function, with a prototype similar to:
    >
    > void * f(data_type type, void * data)
    >
    > where "type" would specify the actual type of the data we are dealing with.
    >
    > Can something along these lines be done? Please remember that the above
    > is just a toy example.


    You can use the preprocessor.

    #define func(type) type* f_##type (type* data) {\
    data->x+=1; \
    return data; \
    }

    func(data_one)
    func(data_two)


    With C++, you'd use templates.

    -Peter

    --
    Pull out a splinter to reply.
     
    Peter Ammon, Apr 10, 2004
    #3
  4. No One

    No One Guest

    On Sat, 10 Apr 2004 19:39:38 +0100, Malcolm wrote:


    > You very rapidly get into a murky area of the C standard here. There are
    > some guarantees about structure layout in memory, but it is generally not
    > a good idea to use extendable structures or to try to convert one type of
    > structure to another.
    >
    > If two structures have a common header then do this
    >
    > struct common
    > {
    > int x;
    > ...
    > };
    >
    > typedef struct
    > {
    > struct common header;
    > ...
    > }data_one;
    >
    > typedef struct
    > {
    > struct common header;
    > ...
    > } data_two;
    >
    > Now when you write f
    >
    > void f(data_type type, void *data)
    > {
    > data_one *data1;
    > data_two *data2;
    > switch(type)
    > {
    > case 1:
    > data1 = data;
    > foo( data1->header);
    > break;
    > case 2:
    > data2 = data;
    > foo(data2->header);
    > break;
    > }
    > }
    > }
    > Theoretically you could cast data to a struct common * directly, but this
    > is asking for trouble. For instance, if someone inserts another member at
    > the beginning of one of the structs the code will break without warning.


    Thanks for your feedback. The solution that you propose is similar to
    what I am using. My actual problem is even worse than I presented, for the
    common fields might not always be the first ones in the structures :-( It
    seems to me that I am stuck with ugly code.
     
    No One, Apr 10, 2004
    #4
  5. The Answer

    Where is Cheema these days?

    --
    Munawar Akram
    -=-
    This message was posted via two or more anonymous remailing services.
     
    Munawar Akram, Apr 10, 2004
    #5
    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. ramu
    Replies:
    2
    Views:
    330
    rlblaster
    Feb 20, 2006
  2. /M
    Replies:
    1
    Views:
    356
    Gianni Mariani
    Nov 14, 2006
  3. Replies:
    10
    Views:
    536
    tragomaskhalos
    Feb 28, 2008
  4. Gary Roach
    Replies:
    0
    Views:
    114
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    122
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page