arcull
26th August 2015, 10:35
Hi.
This post is somewhat related to this onehttp://www.qtcentre.org/threads/63447-A-shortcut-please-%29, but have rather opened new one to avoid confusion. So the question is simple, but the answer is not, at least for me ;) How much do these two approaches differ at all in a techical sence? Which one would you use in which case and why? Consider these two code snippets bellow, got from http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators and http://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm
#include<iostream>
using namespace std;
void swapping(int *ptr_c, int *ptr_d)
{
int tmp;
tmp = *ptr_c;
*ptr_c = *ptr_d;
*ptr_d = tmp;
cout << "In function:\n" << *ptr_c << '\n' << *ptr_d << '\n';
}
void main()
{
int a,b;
a=5;
b=10;
cout << "Before:\n" << a << '\n' << b << '\n';
swapping(&a,&b);
cout << "After:\n" << a << '\n' << b << '\n';
}
// function definition to swap the values.
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
#include <iostream>
using namespace std;
// function declaration
void swap(int &x, int &y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using variable reference.*/
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
This post is somewhat related to this onehttp://www.qtcentre.org/threads/63447-A-shortcut-please-%29, but have rather opened new one to avoid confusion. So the question is simple, but the answer is not, at least for me ;) How much do these two approaches differ at all in a techical sence? Which one would you use in which case and why? Consider these two code snippets bellow, got from http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators and http://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm
#include<iostream>
using namespace std;
void swapping(int *ptr_c, int *ptr_d)
{
int tmp;
tmp = *ptr_c;
*ptr_c = *ptr_d;
*ptr_d = tmp;
cout << "In function:\n" << *ptr_c << '\n' << *ptr_d << '\n';
}
void main()
{
int a,b;
a=5;
b=10;
cout << "Before:\n" << a << '\n' << b << '\n';
swapping(&a,&b);
cout << "After:\n" << a << '\n' << b << '\n';
}
// function definition to swap the values.
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
#include <iostream>
using namespace std;
// function declaration
void swap(int &x, int &y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using variable reference.*/
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}