E
Eli Luong
Hi,
Sorry if I accidentally posted a blank post before.
But, I was wondering, I'm practicing with pointers right now, and
wanted to know if I am using it correctly in the following code. By
correct I mean it follows convention, it should be the way I'm using
pointers to do the swapping thing I'm trying below.
Thanks.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// prototype functions
void swap(double *x, double *y);
int main()
{
double a = 10;
double b = 20;
double *ptr1 = &a;
double *ptr2 = &b;
cout << a << " " << b << endl;
swap(ptr1, ptr2);
cout << a << " " << b << endl;
//system("pause");
return 0;
}
void swap(double *x, double *y)
{
double temp = *x;
*x = *y;
*y = temp;
}
Sorry if I accidentally posted a blank post before.
But, I was wondering, I'm practicing with pointers right now, and
wanted to know if I am using it correctly in the following code. By
correct I mean it follows convention, it should be the way I'm using
pointers to do the swapping thing I'm trying below.
Thanks.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
// prototype functions
void swap(double *x, double *y);
int main()
{
double a = 10;
double b = 20;
double *ptr1 = &a;
double *ptr2 = &b;
cout << a << " " << b << endl;
swap(ptr1, ptr2);
cout << a << " " << b << endl;
//system("pause");
return 0;
}
void swap(double *x, double *y)
{
double temp = *x;
*x = *y;
*y = temp;
}