Thread: Need help with beginner pointer functions.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    18

    Need help with beginner pointer functions.

    Hello I am new here hopefully I won't have broken any rules by the end of this post but I need help with my project. The goal is to include these 3 pointer functions into my code(replacing whatever needs to be replaced) the functions are void halve(int* number) void readAge (int* age), and void writeAge(int age). I don't expect anyone to do it for me but if anyone could give an simple similar example or even work one of them for me that would be amazing. Here is my code(hopefully I get the tag to work). If my indentation is off it's the copy paste but it looks good in the compiler.

    Code:
    int readAge(void);
    
    void writeAge(int age);
    
    int main(void) {
    
       int age;
    
       age = readAge();
       writeAge(age);
    
       printf("Double that time and... \n", age);
       writeAge( 2 * age);
       printf("Wow that's old! \n", age);
    
       printf("Now if I cut your age in half... \n", age);
       writeAge(age / 2);
       printf("Haha you're almost a kid again! \n", age);
    
    return EXIT_SUCCESS;
    
    }
    
    int readAge(void )
    {
       int age;
       printf("How old are you?");
       scanf("%d", &age);
       return age;
    }
    
    void writeAge(int age)
    {
        printf("You are %d!\n\n", age);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Question 4.12

    That should get you started.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mo777 View Post
    Hello I am new here hopefully I won't have broken any rules by the end of this post but I need help with my project. The goal is to include these 3 pointer functions into my code(replacing whatever needs to be replaced) the functions are void halve(int* number) void readAge (int* age), and void writeAge(int age). I don't expect anyone to do it for me but if anyone could give an simple similar example or even work one of them for me that would be amazing. Here is my code(hopefully I get the tag to work). If my indentation is off it's the copy paste but it looks good in the compiler.
    I'd say you're off to a pretty good start. Welcome aboard...

    I've marked your code to make it match your criteria for readAge(int *age) hopefully that will get you off in the right direction...

    Code:
    #include <stdio.h>
    
    void readAge(int *age);
    
    void writeAge(int age);
    
    int main(void) {
    
       int age;
    
       readAge(&age);
    
       writeAge(age);
    
       printf("Double that time and... \n", age);
       writeAge( 2 * age);
       printf("Wow that's old! \n", age);
    
       printf("Now if I cut your age in half... \n", age);
       writeAge(age / 2);
       printf("Haha you're almost a kid again! \n", age);
    
    return EXIT_SUCCESS;
    
    }
    
    void readAge(int *age )
    {
        printf("How old are you?");
       scanf("%d", age);
    }
    
    void writeAge(int age)
    {
        printf("You are %d!\n\n", age);
    }

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Thank you two a ton! Now I am getting somewhere!

    Edit: Ok stop me if I am asking too much but for the halveAge function I keep getting a compiler error stating invalid operands to binary / Here is what that section of the code looks like

    Code:
    /*At the top*/
    void halveAge (int *age);
    
    /*Under main*/
     halveAge(&age);
    
    /*After main*/
    oid halveAge(int *age)
    {
        printf("Now if I cut your age in half... \n", age);
        writeAge(age / 2);
        printf("Haha you're almost a kid again! \n", age);
    }
    I assume it is something simple I messed up on.
    Last edited by Mo777; 02-16-2011 at 10:17 PM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Ugh I hate to do this on a new board but people may not read because of no new posts so bump..

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mo777 View Post
    Ugh I hate to do this on a new board but people may not read because of no new posts so bump..
    Patience! This is not real time communications.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    /*At the top*/
    void halveAge (int *age);
    
    /*Under main*/
     halveAge(&age);
    
    /*After main*/
    void halveAge(int *age)
    {
        printf("Now if I cut your age in half... \n", age);
        writeAge(*age / 2);
        printf("Haha you're almost a kid again! \n", age);
    }
    In this case you are passing in a pointer but need the value at the pointer so you have to "dereference" the pointer to get at the value...

    And yeppers... void does have a v at the beginning

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    I'm sorry I know just freaked out and panicked thought this was due much sooner than it turned out to be and didn't know if anyone would check back after that last message.

    Edit: Thank you soo much your a life saver! I even added a doubleAge function now.
    Last edited by Mo777; 02-16-2011 at 10:45 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mo777 View Post
    I'm sorry I know just freaked out and thought this was due much sooner than it turned out to be and didn't know if anyone would check back after that last message.
    FWIW... it's pushing midnight where I am... You're lucky I answered tonight, at all.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to be compiling with warnings on:
    Code:
    printf("Haha you're almost a kid again! \n", age);
    You aren't actually doing anything with age there.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with beginner pointer functions.
    By Mo777 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2011, 09:38 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. functions and pointer arguments
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2002, 05:04 PM