More pointer help -_-

All of these pointers are starting to confuse me. I am supposed to convert this code from passing by reference to passing by value and pointers. Alright the problem lies in the function Fun1 with the increment operator. My output of what should happen is written on the bottom of my code. For some reason, I cannot increment the value of *a

I posted the original code I am converting at the very bottom

***Converted Code:***

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;

int Fun1( int * a, int * b );
void Fun2( int * a, int * b );
void Fun3( int * c, int * d );
void PrintOutput( int * a, int * b );

int main()
{
    int a = 2, b = 10;

    PrintOutput( &a, &b );

    a = Fun1( &a, &b );

    cout << a << "\t" << b << endl;

    Fun2( &a, &b );
    PrintOutput( &a, &b );

    return 0;
}
// PrintOutput
void PrintOutput( int * a, int * b )
{
   cout << *a << "\t" << *b << endl;
}	
// Fun1
int Fun1( int * a, int * b )
{
   int c;

   c = *a + *b;
   *a++;
   --*b;

   cout << *a << "\t" << *b << "\t" << c << endl;
	
   return c;
}
// Fun2
void Fun2( int * a, int * b )
{
    *a += 5;
    double temp = pow(static_cast<double>(*a), 2);
    *b = static_cast<int>( temp ); 

    PrintOutput( a, b );
    Fun3( a, b );
    PrintOutput( a, b );
}
// Fun3
void Fun3( int  *c, int  *d )
{
    *c = 25;
    *d = 10;

    PrintOutput( c, d );
}
// Output
//2       10
//3       9       12
//12      10
//17      289
//25      10
//25      10
//25      10 



***Code I am converting:***

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;

int Fun1( int a, int b );
void Fun2( int & a, int b );
void Fun3( int & c, int & d );
void PrintOutput( int a, int b );

int main()
{
    int a = 2, b = 10;

    PrintOutput( a, b );

    a = Fun1( a, b );

    cout << a << "\t" << ++b << endl;

    Fun2( a, b );
    PrintOutput( a, b );

    return 0;
}
// PrintOutput
void PrintOutput( int a, int b )
{
   cout << a << "\t" << b << endl;
}	
// Fun1
int Fun1( int a, int b )
{
   int c;

   c = a + b;
   a++;
   --b;

   cout << a << "\t" << b << "\t" << c << endl;
	
   return c;
}
// Fun2
void Fun2( int & a, int b )
{
    a += 5;
    double temp = pow(static_cast<double>(a), 2);
    b = static_cast<int>( temp ); 

    PrintOutput( a, b );
    Fun3( a, b );
    PrintOutput( a, b );
}
// Fun3
void Fun3( int & c, int & d )
{
    c = 25;
    d = 10;

    PrintOutput( c, d );
}
// Output
//2       10
//3       9       12
//12      10
//17      289
//25      10
//25      10
//25      10
Last edited on
When in doubt, use parentheses.
1
2
3
4
5
6
7
8
9
10
// Fun1
int Fun1( int * a, int * b )
{
    int c = *a + *b;
    (*a)++;
    --(*b);

    cout << *a << "\t" << *b << "\t" << c << endl;
    return c;
}
Thanks!
I posted the original code I am converting at the very bottom

The only time you should use pointers in the converted code is where you used references in the original code.
Topic archived. No new replies allowed.