pointer problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void pointer(int* first , int* second , int* third )

{
	int a, b, c;

        first = &a;
	second = &b;
	third = &c;

	*first = b;
	*second = c;
	*third = a;

	

}


my main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()

{

	int first = 1;
	int second = 2;
	int third = 3;

	cout << &first << endl;			//address same

	cout << first << endl;			//a
	cout << second << endl;			//b
	cout << third << endl;			//c

	pointer(&first, &second, &third); //change a b c to b c a (a=b b=c c=a)

	cout << &first << endl;			//address same

	cout << first << endl;			//b
	cout << second << endl;			//c
	cout << third << endl;			//a 


It doesnt seem to work like I want it to work
//change a b c to b c a (a=b b=c c=a)

Why?
1 2 3 should be now 2 3 1
Last edited on
You are being passed addresses of variables whose values you want to swap, correct? You are losing those addresses by overwriting them with the addresses of a, b, and c in your function. I don't think that's what you were meaning to do.
use this
void pointer( int* first , int * second , int* third )

{
int a, b, c;

a=*first;
b=*second;
c=*third;

*first = b;
*second = c;
*third = a;



}
Giving a solution like that is not going to help the TC learn how to program, especially without any explanation. Please don't do this in the future.
closed account (z05DSL3A)
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
34
35
36
37
38
39
40
41
#include <iostream>

void pointer(int * a_ptr, int * b_ptr, int * c_ptr)
{
	// make a copy of the value that a_ptr points to
	int temp = *a_ptr;

	// assign the value that b_ptr points to to the location
	// that a_ptr points to
	*a_ptr = *b_ptr;

	// assign the value that c_ptr points to to the location
	// that b_ptr points to
	*b_ptr = *c_ptr;

	// finally, asign the value stored in temp to the 
	// location pointed to by c_ptr
	*c_ptr = temp;
}

int main()
{
	int first = 1;
	int second = 2;
	int third = 3;

	std::cout << &first << std::endl;			//address same

	std::cout << first << std::endl;			//a
	std::cout << second << std::endl;			//b
	std::cout << third << std::endl;			//c

	pointer(&first, &second, &third); //change a b c to b c a (a=b b=c c=a)

	std::cout << &first << std::endl;			//address same

	std::cout << first << std::endl;			//b
	std::cout << second << std::endl;			//c
	std::cout << third << std::endl;
	return 0;
}
@awawsese765 Thanks man that is what i need
I knew my logic was wrong but my idea is right
Last edited on
@Grey Wolf this even better thanks man
Topic archived. No new replies allowed.