#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *a,*b,*temp;
cout<<“Enter value of a and b:”;
cin>>*a>>*b;
temp=a;
a=b;
b=temp;
cout<<“nAfter swapingna=”<<*a<<“nb=”<<*b;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *a,*b,*temp;
cout<<“Enter value of a and b:”;
cin>>*a>>*b;
temp=a;
a=b;
b=temp;
cout<<“nAfter swapingna=”<<*a<<“nb=”<<*b;
getch();
}
Here is how I rewrote:
//this program will swap two ints using ptrs
#include
using namespace std;
int main()
{
int* a = new int;
int* b = new int;
cout << "Enter value of a and b:";
cin >> *a >> *b;
//put a in temp
int* temp = a;
//put b in a
a = b;
//put temp, was a, into b
b = temp;
cout << "nAfter swapingna=" << *a << "nb=" << *b;
delete a;
delete b;
return 0;
}
int* a = new int;
int* b = new int;
hi can you explain to me what is the use of this part of coding ?
based on what i know,
int* a = new int
– * a means, a is now a pointer pointing at new int.
– new int is to set a side an empty memory space to store input from the user. this is from dynamic memory allocation
Good Effort bro!! Neeraj
Good job Bro! Go on…