Thread: Calling Functions with Pointer Arguments; Using Pointer Variables as Operaators

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    8

    Calling Functions with Pointer Arguments; Using Pointer Variables as Operaators

    I'll apologize in advance--I'm only a few weeks into C programming, and I know this code is terrible.

    There are, or course, better ways to do this, but I need to stick to some rules:

    (1) Use only pointer variables and not arrays or structs.
    (2) Use the three functions shown--regardless of easier methods.

    The program should ask for some input, operate on those numbers, and then display the results. It's easy to do, but not this way and not for a noob!

    I know I am confused over these things:

    (1) All that syntax using '*' and '&' or neither.
    (2) How to use the char type correctly.
    (3) How to use a char type input as an operator (a + b).
    (4) How to use the pointer of the operator variable (+,-,*,/)
    in an actual equation.
    (5) Purdy much everything.

    I may have made this worse by guessing at ways to fix it. I don't even understand some of it--I just tried to go by online examples. I'll appreciate any kind of help. I need to learn this!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    // *** Prototype Functions ***
        void Post_Results   (float*);
        void Calculate        (float*, float*, char*, float*);
        void Get_Numbers   (float*, char*, float*);
    
    
    int main()
    {       float FirstNum, SecNum, Result;
            char  Oper;
    
    
        Get_Numbers     (&FirstNum, &Oper, &SecNum);
        Calculate       (&Result, &FirstNum, &Oper, &SecNum);
        Post_Results    (&Result);
        return 0;
    }
    
    
    /**********Post_Results**********/
    void Post_Results(float *ptr_Result)
    {
        printf("\n\nThe result of your equation is :  %f", *ptr_Result);  }
    
    
    /**********Calculate**********/
    void Calculate(float *ptr_Result, float *ptr_FirstNum, char *ptr_Oper, float *ptr_SecNum)
    {
       *ptr_Result = *ptr_FirstNum *ptr_Oper *ptr_SecNum;  }
    
    
    
    
    /**********Get_Numbers**********/
    void Get_Numbers(float *ptr_FirstNum, char *ptr_Oper, float *ptr_SecNum)
    {   printf("\n\nEnter your equation in the correct format.");
        printf("\nExample:  Number (space) Operand (space) Number [Enter]");
        printf("\n          14.6 + 2.3 [Enter]  ");
        scanf("%f, %c, %f", *ptr_FirstNum, *ptr_Oper, *ptr_SecNum);  }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Your scanf call has these problems:

    1. Don't use commas, just use whitespace
    2. The operands to scanf must be pointer type. Since you pass in float *'s, it means you leave off the * from the parameter.

    Code:
    scanf("%f %c %f", ptr_FirstNum, ptr_Oper, ptr_SecNum);
    Also your Calculate function can't work like this:
    Code:
    *ptr_Result = *ptr_FirstNum *ptr_Oper *ptr_SecNum;
    So you want the *ptr_Oper, which is a char like '+', '-', etc to be replaced as a mathematical expression. This must be done manually like this

    Code:
    if (*ptr_Oper == '+')
      *ptr_Result = *ptr_FirstNum + *ptr_SecNum;
    else if (*ptr_Oper == '-')
      //...
    else {
      printf("Unknown operator!\n");
      exit(123);
    }
    For the notation, start with simple examples and fully understand those ones on their own. For example, in scanf you usually do it like this:

    Code:
    int x;
    scanf("%d", &x);
    So now try it with a pointer parameter x like in your Calculate function.

    Code:
    int *x = malloc(sizeof(*x));
    scanf("%d", x);
    // ...
    free(x);
    Since &*x is the same as x, we just write x.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    scanf() requires addresses as arguments to store the results into. Pointer's values *are* addresses. When you dereference them with *, you are evaluating them to the value contained at that address, rather than passing the address. So get rid of the * on each pointer argument in the scanf in your Get_Numbers function.

    You cannot use a char directly as you attempt to in Calculate as an operator. You will have to evaluate the operator input by the user and with decision branching, apply the appropriate operation the the operands. '+' will be interpreted as a char, and not as an operator.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Thank you. That cleared up a bunch of errors. I'll rework the Calculate function as you described (sorta knew I would have to).

    What would be the syntax for a printf() statement to show the value (not the pointer content) of the variables being passed to each function? That would be a good marker to see what's working.

    Thanks in advance...

    Quote Originally Posted by c99tutorial View Post
    Your scanf call has these problems:

    1. Don't use commas, just use whitespace
    2. The operands to scanf must be pointer type. Since you pass in float *'s, it means you leave off the * from the parameter.

    Code:
    scanf("%f %c %f", ptr_FirstNum, ptr_Oper, ptr_SecNum);
    Also your Calculate function can't work like this:
    Code:
    *ptr_Result = *ptr_FirstNum *ptr_Oper *ptr_SecNum;
    So you want the *ptr_Oper, which is a char like '+', '-', etc to be replaced as a mathematical expression. This must be done manually like this

    Code:
    if (*ptr_Oper == '+')
      *ptr_Result = *ptr_FirstNum + *ptr_SecNum;
    else if (*ptr_Oper == '-')
      //...
    else {
      printf("Unknown operator!\n");
      exit(123);
    }
    For the notation, start with simple examples and fully understand those ones on their own. For example, in scanf you usually do it like this:

    Code:
    int x;
    scanf("%d", &x);
    So now try it with a pointer parameter x like in your Calculate function.

    Code:
    int *x = malloc(sizeof(*x));
    scanf("%d", x);
    // ...
    free(x);
    Since &*x is the same as x, we just write x.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Thank you. That cleared up a bunch of errors. I'll rework the Calculate function as you described (sorta knew I would have to).

    What would be the syntax for a printf() statement to show the value (not the pointer content) of the variables being passed to each function? That would be a good marker to see what's working.

    Thanks in advance...

    Quote Originally Posted by Tclausex View Post
    scanf() requires addresses as arguments to store the results into. Pointer's values *are* addresses. When you dereference them with *, you are evaluating them to the value contained at that address, rather than passing the address. So get rid of the * on each pointer argument in the scanf in your Get_Numbers function.

    You cannot use a char directly as you attempt to in Calculate as an operator. You will have to evaluate the operator input by the user and with decision branching, apply the appropriate operation the the operands. '+' will be interpreted as a char, and not as an operator.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your use of printf was correct. printf expects values not addresses (except for strings). So *pointer de-references the address value of the pointer to get the value at that address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using functions with pointer arguments - Connect 4
    By Thinh Cao in forum C Programming
    Replies: 17
    Last Post: 10-01-2012, 03:34 AM
  2. Replies: 2
    Last Post: 01-29-2012, 09:20 AM
  3. Replies: 5
    Last Post: 07-28-2011, 06:45 AM
  4. Replies: 9
    Last Post: 01-26-2008, 03:12 AM
  5. functions and pointer arguments
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2002, 05:04 PM