I would like to know how can the swapping function be implemented using pointers, void swap (int*, int*)

//function prototypes
void swap(int& a, int& b);

//function (passing by ref)
void swap(int& a, int& b)
{
  int t = a;
  a = b;
  b = t;
}

Is this correct?

//Using pointers
void swap(int *num1, int *num2) 
{
int tempvar1;


tempvar1 = *num1;
*num1 = *num2;
*num2 = tempvar1;

>Is this correct?
Give it a try, the code to test this function is trivial.

I would like to know how can the swapping function be implemented using pointers, void swap (int*, int*)

//function prototypes
void swap(int& a, int& b);

//function (passing by ref)
void swap(int& a, int& b)
{
  int t = a;
  a = b;
  b = t;
}

Is this correct?

//Using pointers
void swap(int *num1, int *num2) 
{
int tempvar1;


tempvar1 = *num1;
*num1 = *num2;
*num2 = tempvar1;

end quote.

Yes that is correct.
When you call the function it will be

swap(&i,&j)

Actually swarping by reference would be a much faster way to change the variables. You do not need to dereference the variables within the function.

An example of a swarp by reference would be

//Using pointers
void swap(int &num1, int &num2) 
{
int tempvar1;


tempvar1 = num1;
num1 = num2;
num2 = tempvar1;

Hope that helps

> Actually swarping by reference would be a much faster way to change the variables.
> You do not need to dereference the variables within the function.
References are not quicker, just safer. The safety aspects are
- a reference can never be NULL (a pointer can be)
- a reference can only be initialised, never assigned. This helps the compiler to enforce the first measure.

The underlying implementation is still a pointer, only the detail is hidden from you.

Eg. Two kinds of swap, both generating identical code.

$ cat foo.cpp
void swap1 ( int &a, int &b ) {
  int temp = a;
  a = b;
  b = temp;
}
void swap2 ( int *a, int *b ) {
  int temp = *a;
  *a = *b;
  *b = temp;
}
$ g++ -c -S foo.cpp
$ cat foo.s
_Z5swap1RiS_:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	movl	8(%ebp), %eax
	movl	(%eax), %eax
	movl	%eax, -4(%ebp)
	movl	12(%ebp), %eax
	movl	(%eax), %edx
	movl	8(%ebp), %eax
	movl	%edx, (%eax)
	movl	12(%ebp), %edx
	movl	-4(%ebp), %eax
	movl	%eax, (%edx)
	leave
	ret

_Z5swap2PiS_:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	movl	8(%ebp), %eax
	movl	(%eax), %eax
	movl	%eax, -4(%ebp)
	movl	12(%ebp), %eax
	movl	(%eax), %edx
	movl	8(%ebp), %eax
	movl	%edx, (%eax)
	movl	12(%ebp), %edx
	movl	-4(%ebp), %eax
	movl	%eax, (%edx)
	leave
	ret
$ g++ -c -S -O2 foo.cpp
$ cat foo.s
_Z5swap1RiS_:
	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %edx
	movl	12(%ebp), %ecx
	pushl	%ebx
	movl	(%edx), %ebx
	movl	(%ecx), %eax
	movl	%eax, (%edx)
	movl	%ebx, (%ecx)
	popl	%ebx
	popl	%ebp
	ret

_Z5swap2PiS_:
	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %edx
	movl	12(%ebp), %ecx
	pushl	%ebx
	movl	(%edx), %ebx
	movl	(%ecx), %eax
	movl	%eax, (%edx)
	movl	%ebx, (%ecx)
	popl	%ebx
	popl	%ebp
	ret

Yes thanks for pointing that out Salem, it is definitely safer to pass by reference to change the value of variables in a function. However, I learn that it is faster in a sense that there is no need for extra memory allocation when using a reference as compared to a pointer. Is it true. I believe i have read it somewhere. Thanks for enlightening me :) I am a programmer still learning!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.