Click here to Skip to main content
15,071,399 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
ranjithsubra20-Sep-16 3:09
professionalranjithsubra20-Sep-16 3:09 
GeneralRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
NotPolitcallyCorrect20-Sep-16 3:26
MemberNotPolitcallyCorrect20-Sep-16 3:26 
GeneralRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
ranjithsubra20-Sep-16 3:33
professionalranjithsubra20-Sep-16 3:33 
AnswerRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
David Crow20-Sep-16 5:22
MemberDavid Crow20-Sep-16 5:22 
GeneralRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
Krishnakumartg20-Sep-16 22:13
MemberKrishnakumartg20-Sep-16 22:13 
AnswerRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
jeron120-Sep-16 5:23
Memberjeron120-Sep-16 5:23 
AnswerRe: In C++ compiler providing the default copy constructor ,then why do we need copy constructor Pin
Saravanan Sundaresan24-Sep-16 19:53
professionalSaravanan Sundaresan24-Sep-16 19:53 
QuestionRight method of swapping arrays and classes using pointers Pin
Javier Luis Lopez19-Sep-16 21:35
MemberJavier Luis Lopez19-Sep-16 21:35 
I have read a lot of stuf of programmers triying to swap arrays using for over every element like here (two pages):

[^]

There also other methods that involves making swapping element by element but it is hidden:
std::swap(array1,array2);

It would be better using pointers, with only 3 operations all the elements are swapped:

double *a,*b; two arrays
double **ptr1,**ptr2,**ptrx;
ptr1=&a;ptr2=&b;
Swapping:
ptrx=ptr1;ptr1=ptr2;ptr2=ptrx;

Example code:

<pre lang="c++">
#include <iostream>

using namespace std;

double *a;
double *b;

class c_class { public: double x; } ;


void main()
{
	//1. Initialize of the array:
	a = new double[7];
	b = new double[7];
	long i;
	for (i = 0; i < 7; i++)
	{
		a[i] = i + 100;
		b[i] = i + 300;
	}

	//2. We need three pointers:
	double **ptr1 = &a,**ptr2=&b,**ptrx;

	//3. Printing initial array values:
	cout << "=== SWAPPING ARRAYS ===" << endl;
	cout << "STEP 1: ptr1= " << endl;
	for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
	cout << " ptr2= ";
	for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
	cout << endl;

	//4. Swapping the arrays in only 3 pointer operations:
	cout << " Swapping the arrays" << endl;
	ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
	//5. Printing the arrays after swapping:
	cout << "STEP 2: ptr1= ";
	for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
	cout << " ptr2= ";
	for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
	cout << endl;

	//6. Swapping again the arrays in only 3 pointer operations:
	cout << " Swapping the arrays" << endl;
	ptrx = ptr1; ptr1 = ptr2; ptr2 = ptrx;
	//7. Printing the arrays after swapping: results must be the same than initial ones:
	cout << "STEP 3: ptr1= ";
	for (i = 0; i < 7; i++) cout << (*ptr1)[i] << " ";
	cout << " ptr2= ";
	for (i = 0; i < 7; i++) cout << (*ptr2)[i] << " ";
	cout << endl;

	cout << "\n\n=== SWAPPING CLASSES ==="<<endl;
	c_class clas1, clas2;
	clas1.x = 1000.1;
	clas2.x = 5000.5;
	//10. Declaration of the pointers:
	c_class *pc1, *pc2,*pcx;
	//11. Initial value of the pointers:
	pc1 = &clas1; pc2 = &clas2;
	//12. Printing initial classes values:
	cout << "Class 1 and 2 x value: " << pc1->x << " "<<pc2->x<<endl;
	//13. Swapping the classes in only 3 steps:
	cout << " Swapping the classes " << endl;
	pcx = pc1; pc1 = pc2; pc2 = pcx;
	cout << "Class 1 and 2 x value: " << pc1->x << " " << pc2->x << endl;


	cout << "\n\n======end========\n"; getchar(); getchar();


The only problem is that the arrays must be declared initially as pointers. If they are declared as:

double a[7];, the vs2013 does mark pointer assigment as an error:

double *ptr=a;

Is there a way to solve it?
AnswerRe: Right method of swapping arrays and classes using pointers Pin
Richard MacCutchan19-Sep-16 22:19
mveRichard MacCutchan19-Sep-16 22:19 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
leon de boer20-Sep-16 0:41
Memberleon de boer20-Sep-16 0:41 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Javier Luis Lopez17-Aug-17 23:11
MemberJavier Luis Lopez17-Aug-17 23:11 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Richard MacCutchan17-Aug-17 23:32
mveRichard MacCutchan17-Aug-17 23:32 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Javier Luis Lopez20-Aug-17 20:19
MemberJavier Luis Lopez20-Aug-17 20:19 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Richard MacCutchan20-Aug-17 21:46
mveRichard MacCutchan20-Aug-17 21:46 
AnswerRe: Right method of swapping arrays and classes using pointers Pin
leon de boer20-Sep-16 15:27
Memberleon de boer20-Sep-16 15:27 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Saravanan Sundaresan24-Sep-16 21:01
professionalSaravanan Sundaresan24-Sep-16 21:01 
QuestionImage processing in C Pin
Ravi Shanakar Singh16-Sep-16 19:23
MemberRavi Shanakar Singh16-Sep-16 19:23 
AnswerRe: Image processing in C Pin
Jochen Arndt16-Sep-16 22:17
professionalJochen Arndt16-Sep-16 22:17 
AnswerRe: Image processing in C Pin
enhzflep18-Sep-16 15:34
Memberenhzflep18-Sep-16 15:34 
GeneralRe: Image processing in C Pin
leon de boer19-Sep-16 7:07
Memberleon de boer19-Sep-16 7:07 
GeneralRe: Image processing in C Pin
enhzflep19-Sep-16 20:48
Memberenhzflep19-Sep-16 20:48 
GeneralRe: Image processing in C Pin
leon de boer19-Sep-16 23:54
Memberleon de boer19-Sep-16 23:54 
AnswerRe: Image processing in C Pin
CPallini18-Sep-16 21:34
mveCPallini18-Sep-16 21:34 
GeneralRe: Image processing in C Pin
leon de boer19-Sep-16 7:09
Memberleon de boer19-Sep-16 7:09 
GeneralRe: Image processing in C Pin
CPallini19-Sep-16 9:14
mveCPallini19-Sep-16 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.