Excerise:
Write a function called cube that returns void, and takes a double*. This function will calculate the cube of the variable passed in by reference. Call the parameter pVariable.
Inside the function, print out the value that is in pVariable, AND the value that it points to. Then calculate the cube of *pVariable.
In the main print out the value that is returned through the parameter. For example, if you pass a variable to the function that contains 3.0, that same variable will contain (the cube 3.0 * 3.0 * 3.0) which is 27.0 after the function is over.
I get this message when I try to run it "Unhandled exception at 0x002b146a in 16-5.exe: 0xC0000005: Access violation writing location 0x00000000."
Code:
#include <stdio.h>
#include <stdlib.h>
/*
3. Write a function called cube that returns void, and takes a double*. This function will calculate the cube of the variable passed in by reference. Call the parameter pVariable.
Inside the function, print out the value that is in pVariable, AND the value that it points to. Then calculate the cube of *pVariable.
In the main print out the value that is returned through the parameter. For example, if you pass a variable to the function that contains 3.0, that same variable will contain (the cube 3.0 * 3.0 * 3.0) which is 27.0 after the function is over.
*/
#include <stdio.h>
#include <stdlib.h>
void cube(double*);
main(){
double a = 3.0;
cube(&a);
system("pause");
}
void cube(double a, double *pVariable){
*pVariable = a * a * a;
printf("The cube of %.2lf is %.2lf\n", a, pVariable);
}