Pointer logic inside functions

Hi, I'm having problems with pointers, I want to make a function that swaps the value of one variable in Main() for another. I have it working within the function, but not back in Main(). Here's what I have so far;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

using namespace std;
void swapS(string first, string second);

int main()
{
	string alpha = "\"This starts the sentence\"", omega = " \"and this is the end\"";
	int wait(0);
	cout<<"The original statement;"<<endl;
	cout << alpha <<omega<<endl<<endl;
	swapS(alpha, omega);
	cout<<"Now after running SwapS and back in main"<<endl;
	cout << alpha <<omega<<endl<<endl;
	cin>>wait;
	return 0;
}

void swapS(string first, string second)
{
			cout<<"this is what happens in swapS before swap;"<<endl;
	cout<<first<<"   "<<second<<endl<<endl;
	string *pfirst = &first, *psecond = &second;
	string hold = first;
		*pfirst = *psecond;
		*psecond = hold;
		cout<<"this is what happens in swapS after swap;"<<endl;
	cout<<first<<"   "<<second<<endl<<endl;
}
So this is the only way I could think of solving it, by inputting the addresses of the variables into the swapS() function. But the added work of creating pointers to input them all just to do a quick swap kind of ruins the convenience. does anyone know how to make a swap function so that you only have to input the variable itself?

Anyway, here's the code that's not so convenient;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

using namespace std;
void swapS(string *pfirst, string *psecond);

int main()
{
	string alpha = "\"This starts the sentence\"", omega = " \"and this is the end\"";
	string *palpha = &alpha, *pomega = &omega;
	int wait(0);
	cout<<"The original statement;"<<endl;
	cout << alpha <<omega<<endl<<endl;
	swapS(palpha, pomega);
	cout<<"Now after running SwapS and back in main"<<endl;
	cout << alpha <<omega<<endl<<endl;
	cin>>wait;
	return 0;
}

void swapS(string *pfirst, string *psecond)
{
			cout<<"this is what happens in swapS before swap;"<<endl;
	cout<<pfirst<<"   "<<psecond<<endl<<endl;

	string hold = *pfirst;
	*pfirst = *psecond;
	*psecond = hold;

		cout<<"this is what happens in swapS after swap;"<<endl;
	cout<<pfirst<<"   "<<psecond<<endl<<endl;
}
closed account (D80DSL3A)
@newbieg. I just posted the same function here. lol
(and then deleted it, no sense in duplication).

Anyways, you forgot to dereference the pointers pfirst and psecond on lines 24 and 30.
Last edited on
All right, I've figured it out for the most part, but if someone has a better version than this please toss me a bone.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;
void swapS(string *pfirst, string *psecond);

int main()
{
	string alpha = "\"This starts the sentence\"", omega = " \"and this is the end\"";
	int wait(0);
	cout<<"The original statement;"<<endl;
	cout << alpha <<omega<<endl<<endl;
	swapS(&alpha, &omega);
	cout<<"Now after running SwapS and back in main"<<endl;
	cout << alpha <<omega<<endl<<endl;
	cin>>wait;
	return 0;
}

void swapS(string *pfirst, string *psecond)
{
			cout<<"this is what happens in swapS before swap;"<<endl;
	cout<<pfirst<<" = "<<*pfirst<<"   "<<psecond<<" = "<<*psecond<<endl<<endl;

	string hold = *pfirst;
	*pfirst = *psecond;
	*psecond = hold;

		cout<<"this is what happens in swapS after swap;"<<endl;
	cout<<pfirst<<" = "<<*pfirst<<"   "<<psecond<<" = "<<*psecond<<endl<<endl;
}
Topic archived. No new replies allowed.