How to Swap Two Numbers using Call by Reference in C

View: 551    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

0 point/1 review File has been tested

How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value. In this post let’s understand what is call by reference and how to swap two numbers using call by reference method.

In Call By Reference method, we pass the address(reference/pointer) of an object instead of it’s value.It means the changes made to the parameter affect the passed argument. By using call by reference method no additional memory is used as we are using pointers. .To pass an address we use (&) ampresand operator.

To declare a pointer in C asterisk(*) operator is used.
int *firstnum; // firstnum is a pointer of int type.
 
swap (&a,&b); // Passing address of a and b.

Program to Swap Two Numbers using Call by Reference

Let’s write a program to swap two numbers using call by reference.

#include <stdio.h>
 
/* Function to swap numbers. */
 
void swap(int *firstnum, int *secondnum){
 
  int temp;
 
  temp = *firstnum;
  *firstnum = *secondnum;
  *secondnum = temp;
 
}
 
main()
{
 
  int a=12,b=13;
 
  /* Pass address of a and b variable. */
 
  swap(&a,&b);
 
  printf("Values of a and b is %d %d",a,b);
}

Important Points

i) In this program i use swap function to swap two numbers.

ii) In case of call by reference method, i have not return anything from swap function. As the variables are referencing memory. If i used call by value method in that case i need to return the firstnum and secondnum variable values.

How to Swap Two Numbers using Call by Reference in C

How to Swap Two Numbers using Call by Reference in C Posted on 05-10-2016  How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value 5/10 551

Comment:

To comment you must be logged in members.

Files with category

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 38    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    What if you have one or more modules, running independently in a system, which should collaborate between each other? Well, they will have to be able to exchange data, organized as events, messages or packets of some sort.

  • How to Swap Two Numbers using Call by Reference in C

    How to Swap Two Numbers using Call by Reference in C

    View: 551    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

  • How to Print Fibonacci Series using Recursion in C, C++

    How to Print Fibonacci Series using Recursion in C, C++

    View: 548    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Write a program to print Fibonacci Series using recursion. Fibonacci series is a very important program in terms of interviews. To make this program little tough interviewer ask to write a fibonacci series code using recursion.

  • How to Implicitly Typed Arrays In C#

    How to Implicitly Typed Arrays In C#

    View: 617    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

  • How to C# Static vs Instance Methods As Event Handlers

    How to C# Static vs Instance Methods As Event Handlers

    View: 423    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Both static and instance methods can be used as event handlers in C#

  • Build Pass By Reference To Method In C#

    Build Pass By Reference To Method In C#

    View: 578    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Like value types such as int, double, char etc. We can also pass a reference variable to the method in C# as its parameter which allows us to pass an object to the method and it is called pass by reference to method in C#.

  • How to C# Continue Statement

    How to C# Continue Statement

    View: 395    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    continue statement skips or exits the current execution of a loop by given condition and keep execution continue for its next iteration unlike break statement it does not exit or terminate from loops, it terminates only its current execution

  • How to Single Dimensional Arrays In C#

    How to Single Dimensional Arrays In C#

    View: 344    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Single Dimensional Arrays in C# store each individual element at their single dimension, position or a specific location, that location is called an array index. Each index describes the position of each element within an array. Each element in an...

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd