Thread: what's wrong with my Swap function?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    what's wrong with my Swap function?

    Good day
    I'm trying to write a swap function code but it's not working
    Code:
    int swapfunction (int a, int b)
    {
        int i;
        int temp;
        a=100;b=50;
        temp=a;
        a=b;
        b=temp;
        int c[2]={a,b};
        return(c[i]);
    
    
    }
    int main ()
    {
       int num1,i, num2;
       int results[2];
       results[2]=swapfunction(num1,num2);
       num1=12; num2=13;
    
    
       for(i=0;i<=1;++i)
    
    
       printf("%d\t",results[i]);
       return(0);
    
    
    }
    Any help will be highly appreciated!!

    Best
    Ama

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can't return arrays by value.

    The normal approach to swapping would be to pass two pointers to the things you want swapped.
    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
    Nov 2016
    Posts
    33
    Thank you for your interest
    I'm new to C and a more detailed answer would be very welcomed!!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This does not change the value in main.
    Code:
    void foo ( int a ) {
      a = 42;
    }
    int main ( ) {
      int a = 0;
      foo(a);
      // a is still 0
    }
    This changes a in main, but you can only return one thing (integral types, float types, pointers, structs, unions) at a time.
    Code:
    int foo ( ) {
      return 42;
    }
    int main ( ) {
      int a = 0;
      a = foo();
      // a is now 42
    }
    This also changes a in main, but using a pointer.
    Code:
    void foo ( int *a ) {
      *a = 42;
    }
    int main ( ) {
      int a = 0;
      foo(&a);
      // a is now 42
    }
    Knowing that you can have as many parameters as you like(*), consider what this might mean
    void swap ( int *a, int *b )





    (*) the C99 standard states that a compiler should support at least 127 parameters.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swap Function - How to
    By Khabz in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2013, 08:48 AM
  2. swap function
    By c_lady in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 02:27 PM
  3. Good old 'Swap' function
    By starcatcher in forum C Programming
    Replies: 29
    Last Post: 02-03-2008, 10:38 AM
  4. swap function
    By SpEkTrE in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 01:38 PM
  5. Swap Function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-09-2002, 11:11 AM

Tags for this Thread