0

For example, assume I have something like this:

typedef struct one
{
    int data;
    int dogs;
}One;

typedef struct two
{
    int data;
    int birds;
}Two;

typedef struct three
{
    int data;
    int cats;
}Three;

void swapData(???* elem1, ???* elem2) //<- the problem is here
{
    int temp = elem1->data;
    elem1->data = elem2->data;
    elem2->data = temp;
}

int main()
{
    One dog;
    Two bird;
    Three cat;

    dog.data = 1;
    bird.data = 2;
    cat.data = 3;

    swapData(&dog, &cat);
    swapData(&dog, &bird);
    swapData(&bird, &cat);
    return 0;
}

What do I have to put in place of the ??? for the function to accept pointers to either One, Two or Three structs as arguments? I tried void but then I can't access the structs' fields...

3
  • All struct contain two ints..why do you need 3 seperate ones?
    – GoldRoger
    May 12, 2014 at 4:31
  • I didn't quite understand what you're asking but this is just an example and the core of my question isn't related to that.
    – AX2
    May 12, 2014 at 4:34
  • You haven't fully initialized your structures. You are trading on thin ice (at best — it is probably undefined behaviour) trying store a dog in a structure for cats and a cat in a structure for dogs, which is what you'd be doing if you managed to write the swapData function. If the structures were not all the same size and same basic layout, you're be really hosed. But why do you need to do species-changes on your pets? What you're up to is fundamentally misguided, IMNSHO. May 12, 2014 at 5:22

5 Answers 5

1

If you always define the data first in each struct, and data is always an int, then you can do something like this:

void swapData(void* elem1, void* elem2) //<- the problem is here
{
    int* e1 = (int*) elem1;
    int* e2 = (int*) elem2;
    int temp = e1[0];
    e1[0] = e2[0];
    e2[0] = temp;
}

the reason this works is because when you pass a void *, you get memory of size sizeof(One) or whatever you put in. since the data is first, we can just pretend that you passed in an array of integers, and take the first one. If you had a more complicated structure, you could build a struct where the first few elements are the same types as any struct you might pass in. this way you could manipulate multiple objects. that would look something like this:

typedef struct {
    int data;
    char * name;
    ...
} complicated;

typedef struct {
    int data;
    char * name;
    int something_else;
} simple;

typedef struct { // least common denominator
    int data;
    char * name;
} common;

void swap(void * e1, void* e2){
    common* c1 = (common*) e1;
    common* c2 = (common*) e2;
    int tmp = c1->data;
    char * tmpn = c1->name;
    c1->data = c2->data;
    c1->name = c2->name;
    c2->data = tmp;
    c2->name = tmp;
}

hope this makes sense.

1
  • Perfect! The second part covered pretty much my case :) I had the feeling I should be using void* but I didn't really know how to 'convert' (cast?) the stuff. Now I know, thank you :D
    – AX2
    May 12, 2014 at 5:49
0

You can't do that, you have ti write 3 separate functions to achieve this...

void swapData13(one *elem1, three*elem2) 
{
    int temp = elem1->data;
    elem1->data = elem2->data;
    elem2->data = temp;
}

void swapData12(one *elem1, two*elem2)
{
    int temp = elem1->data;
    elem1->data = elem2->data;
    elem2->data = temp;
}

void swapData23(two *elem1, three*elem2) 
{
    int temp = elem1->data;
    elem1->data = elem2->data;
    elem2->data = temp;
}

Also the way you are passing parameter in main is wrong... You can do...

swapData13(&dog, &cat);
swapData12(&dog, &bird);
swapData23(&bird, &cat);

Out of curiosity, what do you want to achieve by writing such code?

1
  • As I said, it's an example. Ignore the silliness of the code. Isn't there any way at all to use a single function?
    – AX2
    May 12, 2014 at 4:37
0

To be clear I dont understand exact need of this, But if there are only 3 cases, then you can encode them something like this,

void swapData(void* elem1,  void* elem2, int type) 
{
    if( type == 0 )         //type == 0 corresponds to comparison of One & Two
    {
        One *e1 = (One*)elem1;
        Two *e2 = (Two*)elem2;
        int temp = e1->data;
        e1->data = e2->data;
        e2->data = temp;
    }
    else if(type == 1)      //type == 1 corresponds to comparison of One & Three
    {
        One *e1 = (One*)elem1;
        Three *e2 = (Three*)elem2;
        int temp = e1->data;
        e1->data = e2->data;
        e2->data = temp;
    }
    else
    {
        //type == 2 corresponds to comparison of Two & Three
    }

}

Call from the main function using third argument

swapData(&dog,&bird,0);

This method is not that efficient for higher cases

0

Create another data structure like this:

struct typee{
    int ele1;
    int ele2;
}id;

While filling elements:

id.ele1 = DOGS; // #define DOGS 1
id.ele2 = CATS; // #define CATS 2

Function definition:

void swapData(void* elem1, void* elem2, struct typee id) //<- addind new argument
{
      //using id, type cast the ele1 and ele2 to theier structures 
      ...

}

My suggestion:

typedef struct generic    //only One structure at all
{
    int data;
    int specie;
}all;

main()
{
    struct generic dog;
    struct generic cat;

    ....   // filling structure elements
    swapData(&dog, &cat);
    ...
}

Function definition:

void swapData(struct generic * elem1, struct generic* elem2)
{
    int temp = elem1->data;
    elem1->data = elem2->data;
    elem2->data = temp;
}
0

There are several options available. Among them:

  • Switch to C++. You can then derive your three structs from a common base struct that includes the data member. Or use classes instead of structs and do the same thing. (This isn't as unhelpful as it might sound -- most C compilers these days also compile C++, and switching may be as simple as changing a file extension or compiler flag.)

  • Use a single struct containing a union for the second members. That way, you have only one type to deal with.

  • Use a single struct without the union and name the second member animals.

  • Create a struct Data that holds the data member, and then "piggyback" any other members onto that. That is, make sure that an instance of the Data struct is the very first member of One, Two, and Three. Then use type struct Data * when you only want to refer to the common part.

  • Look into ways to simulate OO-style polymorphism in C. Warning: it's not pretty.

  • Pass a pointer to the data member in each struct rather than a pointer to the entire struct.

Here's an example of that last strategy:

void swapData(int* data1, int* data2)
{
    int temp = *data1;
    *data1 = *data2;
    *data2 = temp;
}

// call it like this:
swapData(&(dog->data), &(cat->data));

Any way you slice it, the swapData() function needs to know what it's dealing with. Plain old C doesn't provide inheritance polymorphism, so there's not a straightforward way to create a single base type that encompasses all three of your structs. So, you have to resort to passing just part of a struct, or casting the struct to a simpler type, etc. Since the data member is actually the first item in each of the structs, you can use the swapData() I provided above but simplify the call a little bit:

swapData((int*)dog, (int*)cat);

That's sneaky, though, and harder to understand (or maybe easier to misunderstand) even though it's shorter.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.