Thread: HOWTO swap sturct or union?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    HOWTO swap sturct or union?

    sturct or union is given. how to swap it? exchange each data???
    Code:
    #include <stdio.h>
    typedef struct {
    	int k;
    	int s;
    	int f;
    }sfType;
    
    void exchange(int *x, int *y)
    {
    	int t = *x;
    	*x = *y;
    	*y = t;
    }
    
    void exchangeSF(sfType *sf1, sfType *sf2)
    {
    	exchange(&sf1.k, &sf2.k);
    	exchange(&sf1.s, &sf2.s);
    	exchange(&sf1.f, &sf2.f);
    }
    ...........
    int main()
    {.........}
    GCC said that It wasn't a structure or union in exchange(&sf1.k, &sf2.k), exchange(&sf1.s, &sf2.s) and exchange(&sf1.f &sf2.f);
    Last edited by Mathsniper; 06-10-2005 at 08:13 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Just use a temp variable of that type

    Code:
    struct foo a, b, temp;
    temp = a;
    a = b;
    b = temp;
    Just like you'd do with anything else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    and here is one more example
    Code:
    #include<stdio.h>
    
    struct student
    {
        char name[20];
        int age;
    };
    
    int main()
    {
        struct student s1={"Somename1",20};
        struct student s2={"somename2",21},temp;
        
        temp=s1;
        s1=s2;
        s2=temp;
        
        printf("S1 - %s and his/her age -> %d\n",s1.name,s1.age);
        printf("S2 - %s and his/her age -> %d\n",s2.name,s2.age);
        
        getchar();
        return 0;
    }
    s.s.harish

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    could you tell me how does gcc implement exchanging structure or union action?

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    If you have to identical struct datatypes but different varaibles use memcpy.

    Code:
    typedef struct
    {
           int a;
           int b;
    } BLAH;
    ................
    
    void BLAH_swap(BLAH *a, BLAH *b)
    {
         BLAH tmp={0,0};
         memcpy(&tmp,a, sizeof(BLAH) );
         memcpy(a,b,sizeof(BLAH) );
         memcpy(b, &tmp, sizeof(BLAH) );
    That what you mean by exchange?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. "generic" union or something
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 09:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 10:12 AM