Thread: Functions with Pointers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    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. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your prototype does not match your definition. The compiler should have warned.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    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. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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.
    Last edited by cpjust; 03-26-2009 at 05:32 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    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. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    [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. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    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);
    }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by krazyxazn View Post
    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...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by caroundw5h View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    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.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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;
    }

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Adak View Post
    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.
    In real life, that's probably true, but for a school assignment, that's one of the requirements.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM