answersLogoWhite

0

Swap two number using pointer

Updated: 8/11/2023
User Avatar

Wiki User

7y ago

Best Answer

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default.

In C, to swap two variables using pass by value:

void swap (int* p, int* q) {

int t = *p;

*p = *q;

*q = t;

}

In C++, to swap two variables using pass by reference:

void swap (int& p, int& q) {

std::swap (p, q);

}

Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments:

void swap (int* p, int* q) {

*p^=*q^=*p^=*q;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Swap two number using pointer
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Coding in c plus plus to swap two nos using pointer?

void swap (int &pa, int &pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }


Swapping of number with using third variable?

To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


How do you swap two variables using airthematic operators?

a += b; b -= a; a -= b;


Swap two numbers using call by value and call by reference?

You cannot swap two numbers using call by value, because the called function does not have access to the original copy of the numbers.Swap with call by reference... This routine uses exclusive or swap without temporary variable.void swap (int *a, int *b) {*a ^= *b;*b ^= *a;*a ^= *b;return;}


How do you swap two numbers without using third one using pointers?

How do you do. I am doing well thank you. Swap two number by using reference operators A tough assignment, it will make you think. I think you are confusing reference operators with pointers. Were I you I would study the section on pointers in your text book or course material.


How develop c program that swap two number with out using temporary variable?

void main() { int a=2,b=5; b=a+b; a=b-a; b=b-a; getch(); }


Is the difference of two natural number is positive?

Not necessarily. If you swap them, the difference will be negative.


Write flowchart searching algorithm?

flow chart to swap two number


The Pointer Sisters number one hits?

They had None. They had two songs that reached number 2: Fire and Slow Hand


Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer