returning 2 values by passing pointers to function

I'm trying to pass pointers to a function so I can get 2 values.


I get garbage in the output
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>
#define PI 3.14159
using namespace std;


void arithmetic(int, int, float*, float*);



int main()
{
    int inHeight; //height
    int inRadius; //radius
    cout > inHeight;
    cout > inRadius;

    float calcArea; //holds Area calculation
    float calcVolumn; //holds Volume calculation
    float *pArea = &calcArea; // stores address of area calculation
    float *pVolumn = &calcVolumn; // stores address of volume calculation

    arithmetic(inHeight,inRadius,pArea,pVolumn); // function call

    
cout << "Radius is: " << inRadius << ",\t" << "Height is: " << inHeight
         << "\n" << "*** using the variables ***\n" << "area: "
         << calcArea << "\t\n" << "volumn: " << calcVolumn << "\n"
         << "*** using the pointers ***\n" << "area: " << *pArea << "\t\n"
         << "volumn: " << *pVolumn;

}

//whats wrong with this
void arithmetic(int inHeight, int inRadius, float* pA, float* pV)
{
    float cylinderVolumn = PI * (inRadius * inRadius) * inHeight; //calculates volume
    float cylinderArea = (2.0 * PI * inRadius * inHeight) * (2.0 * PI *(inRadius * inRadius)); // calculates surface area
    pV = &cylinderVolumn; //stores volume in pV
    pA = &cylinderArea;  //stores area in pV
}


1. Declare 2 ints named inHeight and inRadius

2. ask the user for 2 ints and store the values in inHeight and inRadius

3. declare the following 2 pointers
a. float * pArea
b. float * pVolumn

4. write a function with the following header
void arithmetic(int, int, float*, float*)

5. pass inHeight and inRadius and the 2 pointers to the function

6. in the function, calculate the volume and surface area of the cylinder and put the corresponding answer into the proper pointer address

7. output should look like the following and be printed from main(
Last edited on
Not sure maybe its because cylinderVolumn and cylinderArea is destroyed after the function end, so the values are gone
Try to directly put the value to the pointer not the address so it'll looks like *pV=cylinderVolumn
Or if my guess about the varables iiis destroyed when function end is right, try to use static variable
Last edited on
*pV=cylinderVolumn doesn't work I already tried it. Mind giving me an example of a static variable? is it using const?
closed account (E0p9LyTq)
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
#include <iostream>

void myFunc(int* pA, int* pB);

int main()
{
   int aValue = 0;
   int* pA = &aValue;
   int bValue = 0;
   int* pB = &bValue;

   std::cout << *pA << "  " << *pB << "\n";

   myFunc(pA, pB);

   std::cout << *pA << "  " << *pB << "\n";

   return 0;
}

void myFunc(int* pAValue, int* pBValue)
{
   int a = 15;
   int b = 155;

   *pAValue = a;
   *pBValue = b;
}


0  0
15  155
closed account (E0p9LyTq)
Change your function to this:

1
2
3
4
5
6
7
void arithmetic(int inHeight, int inRadius, float* pA, float* pV)
{
   float cylinderVolumn = PI * (inRadius * inRadius) * inHeight; //calculates volume
   float cylinderArea = (2.0 * PI * inRadius * inHeight) * (2.0 * PI *(inRadius * inRadius)); // calculates surface area
   *pV = cylinderVolumn; // notice the change
   *pA = cylinderArea;   // notice the change
}


What you were doing with pV = &cylinderVolumn is assign the ADDRESS of cylinderVolumn to the pointer. When the function returned the two local variables became invalid.
Last edited on
*pV = cylinderVolumn did work thanks to you all!
Last edited on
closed account (E0p9LyTq)
You're welcome. Pointers can be one of the more difficult parts of C/C++.
Topic archived. No new replies allowed.