1. ## Functions with Pointers

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);
}```

2. Your prototype does not match your definition. The compiler should have warned.

3. Well your cube method accepts two arguments, and you only pass it one. Passing the address of a (&a) IS passing a pointer. So you need only pass the pointer and then dereference it to get the value. Of course, you could just pass the double, but I think you're trying to learn how to use pointers - so that wouldn't help you much!

By accepting the double and a pointer and only defining one of them, referring to the one that isn't provided, you refer to a random place in memory. That's why you get an error - the OS knows you shouldn't be able to do that.

4. How would you suggest I fix it? I just started on pointers and I now understand that I'm suppose to (&variable) when I want to get back a value from the function. But how do I rewrite my arguments and parameters for this program to work?

Thanks for the replies.

5. Code:
`void cube(double a, double *pVariable){`
Get rid of that, then use *pVariable where you use a now.
Also, re-read the second sentence of the question and change the printf() in your cube() function to do what it asks.

6. I believed I solved it. Thanks for the help. How would you guys/girls solve this in your own way using C.

Code:
```#include <stdio.h>
#include <stdlib.h>

void cube(double*);

main(){

double a = 3.0;

cube(&a);
printf("The cube of 3 is %.1lf\n", a);

system("pause");
}

void cube(double *pVariable){
*pVariable = *pVariable * *pVariable * *pVariable;
printf("pVariable has %.1lf\n", *pVariable);
printf("The address of pVariable is: %p\n", &pVariable);
}```
Output:
Code:
```pVariable has 27.0
The address of pVariable is: 0048F95C
The cube of 3 is 27.0
Press any key to continue . . .```

7. [QUOTE=krazyxazn;847917]I believed I solved it. Thanks for the help. How would you guys/girls solve this in your own way using C.

Code:
```#include <stdio.h>
#include <stdlib.h>

void cube(unsigned long*);

int main(void)  {

unsigned long num = 3;  //no cube will be a fraction :)

printf("The cube of 3 is %lu \n", cube(&num));

printf("\n\n\t\t\t      press enter when ready \n");
num = getchar();
return 0;
}

void cube(unsigned long *pnum)  {
*pnum = *pnum * *pnum * *pnum;  //more than 3 times it gets a for loop
printf("pnum cubed is %lu \n", *pnum);
printf("The address of pnum is: %p\n", &pnum);
}```
Not much different. Since I don't have your requirements however, I wouldn't normally make a function just to cube a number.

8. Adak I've been away from coding for awhile so i may be missing something, however your code doesn't compile. It may have something to do with how you called cube() and put it in the print statement. you should try and compile it.

Also calling the function from printf is a little redundant since cube() already calls it to ouput data to stdout. I'm also wondering why you included <stdlib.h> in the code, what function were you calling from there? I'm gonna go with you left it in as an oversight when the OP called system() which is a NO NO in C and C++ programming. Along with the return value of main. lol, good think salem didn't see that OP.

as well the requirements of the OP is to accept a double as argument so we kinda have to go along those guidelines. I've modified your program somewhat, its a nice way to solve the problem. of course we could make it more user friendly by having the user input the number to be cubed.

Code:
```#include <stdio.h>

void cube(double *);

int main(void)  {

double num = 3;
cube(&num);

printf("press enter to exit");
getchar();
return 0;
}

void cube(double *pnum)  {
*pnum = (*pnum) * (*pnum) * (*pnum);

printf("pnum cubed is %.2lf \n", *pnum);

printf("The address of pnum is: %p\n", &pnum);
}```

9. Originally Posted by krazyxazn
I believed I solved it. Thanks for the help. How would you guys/girls solve this in your own way using C.

Code:
```#include <stdio.h>
#include <stdlib.h>

void cube(double*);

main(){

double a = 3.0;

cube(&a);
printf("The cube of 3 is %.1lf\n", a);

system("pause");
}

void cube(double *pVariable){
*pVariable = *pVariable * *pVariable * *pVariable;
printf("pVariable has %.1lf\n", *pVariable);
printf("The address of pVariable is: %p\n", &pVariable);
}```
Are you sure that's what the question is asking?
...print out the value that is in pVariable...

10. why do you need pointers?

Code:
```double cube (double val);
int main(void)
{
double val,res;
scanf("%lf", &val);

res = cube(val);
printf("Cube of %f is %f\n", val,res);
return 0;
}

double cube(double val)
{
return val*val*val;
}```

11. Originally Posted by caroundw5h
Adak I've been away from coding for awhile so i may be missing something, however your code doesn't compile. It may have something to do with how you called cube() and put it in the print statement. you should try and compile it.

Also calling the function from printf is a little redundant since cube() already calls it to ouput data to stdout. I'm also wondering why you included <stdlib.h> in the code, what function were you calling from there? I'm gonna go with you left it in as an oversight when the OP called system() which is a NO NO in C and C++ programming. Along with the return value of main. lol, good think salem didn't see that OP.

as well the requirements of the OP is to accept a double as argument so we kinda have to go along those guidelines. I've modified your program somewhat, its a nice way to solve the problem. of course we could make it more user friendly by having the user input the number to be cubed.

Code:
```#include <stdio.h>

void cube(double *);

int main(void)  {

double num = 3;
cube(&num);

printf("press enter to exit");
getchar();
return 0;
}

void cube(double *pnum)  {
*pnum = (*pnum) * (*pnum) * (*pnum);

printf("pnum cubed is %.2lf \n", *pnum);

printf("The address of pnum is: %p\n", &pnum);
}```
Remember that there is NO such formatting option as %lf. For printf, there is ONLY %f.

12. Originally Posted by Elysia
Remember that there is NO such formatting option as %lf. For printf, there is ONLY %f.
There isn't in C89 - in C99, which supports "long double", there is! Of course, it is undefined in C89, so what happens there is entirely unpredictable, and if we are looking at a C99-compatible system, then long double is expected to be passed in, and if the parameter is ACTUALLY a double, then it's just going to be horribly incorrect (or cause a crash)!

--
Mats

13. There isn't in C89 - in C99, which supports "long double", there is! Of course, it is undefined in C89, so what happens there is entirely unpredictable, and if we are looking at a C99-compatible system, then long double is expected to be passed in, and if the parameter is ACTUALLY a double, then it's just going to be horribly incorrect (or cause a crash)!
oversight on my part, i simply copied the OP code and left in the long float. Although my compiler is c99 compliant - somewhat - it was meant to be "%f" simply. thanks for reminding us of that.

14. The other version compiled and ran for me, but wouldn't call the cube function. I wouldn't have a separate function just to cube one number, anyway.

Code:
```#include <stdio.h>

int main(void)  {
unsigned long num = 3;  //no cube will be a fraction

printf("%lu cubed is %lu \n", num, (num * num * num));
printf("The address of pnum is: %p\n", &num);

printf("\n\n\t\t\t      press enter when ready \n");
num = getchar();
return 0;
}```