How to Swap Two Numbers using Call by Reference in C

View: 656    Dowload: 0   Comment: 0   Post by: hanhga   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. 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 , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

Posted on 05-10-2016 

Comment:

To comment you must be logged in members.

Files with category

  • C# - Simple Phone Directory System

    C# - Simple Phone Directory System

    View: 8    Download: 0   Comment: 0

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

    Simple Phone Directory System Please make scan first using your antivirus. thanks.

  • School Management System

    School Management System

    View: 16    Download: 1   Comment: 0

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

    his School Management Software is Best School Management System written in C#.Net 4.0 as front end and SQL Server 2008as Back end.We can also manage Multiple branches of a School by This Software ... Requirement : Visual Studio 2010/2012/2013/2015...

  • C and C++ SDSSU compilation

    C and C++ SDSSU compilation

    View: 35    Download: 0   Comment: 0

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

    This is the compilation of my 1st year output in C and C++ programming languages. It contains some basic activity like addition, subtraction, multiplication, division and other educational information that can improve and enhance the skills of our...

  • Enrollment and Billing System in C#

    Enrollment and Billing System in C#

    View: 30    Download: 2   Comment: 0

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

    Enrollment and Billing System This is a project entitled Enrollment and Billing System written in C# and SQL Server Management Studio R2 2008 database for Christian Kiddie Care. The system has the following features: Transactions Add Student Add...

  • SqlCommand to T-SQL Printer

    SqlCommand to T-SQL Printer

    View: 292    Download: 0   Comment: 0

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

    In this article, we are going to see a log printer working with Entity Framework that converts SqlCommand to a completely executable T-SQL statement, including all parameter declarations and write to log file. Once a command is executed, You will see...

  • Windows Clipboard Formats

    Windows Clipboard Formats

    View: 125    Download: 0   Comment: 0

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

    This reference gives you an overview of standard and common clipboard formats with links to the data structures.

  • Insert 1 element at the beginning of an array managed by a pointer without moving

    Insert 1 element at the beginning of an array managed by a pointer without moving

    View: 103    Download: 0   Comment: 0

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

    Insert 1 element at the beginning of an array managed by a pointer without moving

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 190    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.

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