Passing of pointers of different types to a function using singleparameter at same time.

Discussion in 'C Programming' started by manu, Sep 16, 2009.

  1. manu

    manu Guest

    Hi all,

    I have a query regarding the passing of pointers of different types to
    a function using single parameter at same time.

    Suppose i have

    struct type1 abc;
    struct type2 xyz;

    My intention is to pass the address of the structures to a function.

    I tried

    - Before the invocation a void pointer array is initialised.

    void *ptr[2] = {
    (void *)&abc,
    (void *)&xyz
    }

    Invoked the function
    fun1(ptr);

    BTW the function signature is fun1(void *vptr)

    In this case wont be able to re-direct the void pointer to get the
    individual pointers. I tried with character pointers but they are
    giving warnings.

    Please explain if any method exists. Hoping for replies.
     
    manu, Sep 16, 2009
    #1
    1. Advertisements

  2. manu

    Mark Bluemel Guest

    [I've slightly reordered his post, for what I hope is more clarity]
    It's often more helpful to explain what you are trying to achieve
    rather than just telling us how you are trying to achieve it. I can't
    easily see what problem you are trying to address.
    I think you mean the addresses not the address....
    Why? If you want to pass multiple addresses (the addresses of two
    structures), why would the function be coded for one address?
    so the type of ptr is "void **" (or "void * []") - right? This is not
    the type that fun1 is coded to receive.
    Code fun1 to take (an) appropriate argument(s). e.g. "void fun1(void
    **ptrs)"
     
    Mark Bluemel, Sep 16, 2009
    #2
    1. Advertisements

  3. When the pointers come in as void*s, you need some kind of way to figure
    out what to cast them to.

    One option is to do something like this:

    #define STRUCT_TYPE1 1
    #define STRUCT_TYPE2 2

    void fun2(void *p, int type)
    {
    struct type1 *pt1;
    struct type2 *pt2;

    switch (type) {
    case STRUCT_TYPE1:
    pt1 = p;
    ...
    break;
    case STRUCT_TYPE2:
    pt2 = p;
    ...
    break;
    }
    }

    ...
    fun2(&abc, STRUCT_TYPE1);
    fun2(&xyz, STRUCT_TYPE2);

    Another option is to embed the type information in the structs:

    struct type0 {
    int type;
    };

    struct type1 {
    int type;
    int payload;
    };

    struct type2 {
    int type;
    float fpayload;
    };

    void fun3(struct type0 *p)
    {
    struct type1 *pt1;
    struct type2 *pt2;

    switch (p->type) {
    case STRUCT_TYPE1:
    pt1 = (struct type1*)p;
    ...
    break;
    case STRUCT_TYPE2:
    pt2 = (struct type2*)p;
    ...
    break;
    }
    }

    ...
    abc.type = STRUCT_TYPE1;
    abc.payload = 3490;
    fun3((struct type0 *)&abc);

    xyz.type = STRUCT_TYPE2;
    xyz.fpayload = 3.14159;
    fun3((struct type0 *)&xyz);

    Hopefully I haven't screwed any of that up over my morning coffee.

    Another poster noted that you were actually passing the wrong type into
    the function, as well. I don't know what your intent was.

    -Beej
     
    Beej Jorgensen, Sep 16, 2009
    #3
  4. manu

    Paul N Guest

    Since you are trying to pass an array of pointers, it's probably
    easier to pass an array of pointers to void. The following code does
    that for fun1. But, as a pointer to most things can be converted to a
    pointer to void and back, it is possible to use a pointer to void if
    you really must do it that way. fun2 and fun3 do this - one uses an
    extra variable in the converting back, to make things a little less
    messy, while the other goes the whole hog.


    #include <stdio.h>

    struct type1 { int a; int b; };
    struct type2 { char x; char y; };

    struct type1 abc;
    struct type2 xyz;

    void fun1(void *vptr[]) {
    printf("b is %d, y is %c\n",
    ( (struct type1 *) vptr[0] ) -> b,
    ( (struct type2 *) vptr[1] ) -> y);
    }

    void fun2(void *vptr) {
    void **vpp = vptr;
    printf("b is %d, y is %c\n",
    ( (struct type1 *) vpp[0] ) -> b,
    ( (struct type2 *) vpp[1] ) -> y);
    }

    void fun3(void *vptr) {
    printf("b is %d, y is %c\n",
    ( (struct type1 *) (((void **) vptr)[0]) ) -> b,
    ( (struct type2 *) (((void **) vptr)[1]) ) -> y);
    }


    int main(void) {

    void *ptr[2] = {
    (void *)&abc,
    (void *)&xyz };

    abc.a = 10;
    abc.b = 20;
    xyz.x = 'X';
    xyz.y = 'Y';


    fun1(ptr);
    fun2(ptr);
    fun3(ptr);

    return 0;
    }


    But, as Beej pointed out, if you don't know what sorts of struct you
    will be passing, then you need some way to tell the functions.
    Whereas, if you do know, you could just pass them by the type that
    they are. So do have a think about what you are trying to do.

    Hope this helps.
    Paul.
     
    Paul N, Sep 16, 2009
    #4
  5. manu

    manu Guest

    Hi,
    The mistake i did was on type of the argument of the function. Thanks
    Paul,Beej and Mark for the clarification.
     
    manu, Sep 17, 2009
    #5
  6. Could you use a struct? It's best to avoid void* if you can

    struct type1 abc;
    struct type2 xyz;

    struct args
    {
    struct type1 *t1;
    struct type2 *t2;
    };

    struct args my_args[] = {&abc, &xyz};

    void fun (struct args formal_args[]);

    int main (void)
    {
    fun (args);
    return 0;
    }
     
    Nick Keighley, Sep 17, 2009
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.