1

I am just doing a revision task and was running into the error too few arguments to function, so I did some research and apparently can use NULL for the statement not in use. However, when I run my program all that happens is the prompt for the user to enter the 3 values then it ends. It doesn't perform the calculations.

Below is my code, if anyone can help with that, it would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>

float res_calculation (float r1, float r2, float r3, float* RS, float* RP )
{
    *RS = (r1+r2+r3);
    *RP = (r1*r2*r3)/(r1*r2+r2*r3+r1*r3);
    
    
    return 0;
}

int main()
{

    float r1,r2,r3,rs,rp;
    printf("Please enter the values for r1, r2 and r3 seperated by a space");
    scanf ("%f %f %f", &r1, &r2, &r3);
    
    res_calculation(r1,r2,r3,&rs,NULL);
    printf("%f",rs);
    
    res_calculation(r1,r2,r3,&rp,NULL);
    printf("%f",rp);
    
    
    return 0;
}
1
  • 3
    NULL is a macro, not a statement. Commented Dec 24, 2020 at 17:11

4 Answers 4

5

You're passing NULL for the RP parameter of res_calculation. This parameter is a pointer which the function subsequently dereferences. So if you pass a NULL pointer, the function will attempt to dereference a NULL pointer which is undefined behavior.

You need to pass a valid pointer value for this parameter, i.e.

res_calculation(r1,r2,r3,&rs,&rp);
Sign up to request clarification or add additional context in comments.

2 Comments

Or, inside the function, test if (RP != NULL) *RP = ….
Thank you, I had previously had my res_calculation as you mentioned above but then i would get the error, 'too few arguments to function'.
0

If a NULL pointer to the function parameter could mean "I don't care for a result stored here", then test for NULL in the function.

float res_calculation (float r1, float r2, float r3, float* RS, float* RP ) {
    if (RS) *RS = (r1+r2+r3);
    if (RP) *RP = (r1*r2*r3)/(r1*r2+r2*r3+r1*r3);
    return 0;
}

Alternatively, code could use a compound literal for temporary storage.

res_calculation(r1, r2, r3, &rs, &(float){0});
printf("%f", rs);

res_calculation(r1 ,r2, r3, &(float){0}, &rp);
printf("%f", rp);

Comments

0

You should do something like that:

float res_calculation(float r1, float r2, float r3, float* RS, float* RP)
{
    *RS = (r1 + r2 + r3);

    if(RP != NULL)
        *RP = (r1 * r2 * r3) / (r1 * r2 + r2 * r3 + r1 * r3);

    return 0;
}

so that if RP is equal to NULL it will not be considered by the function.

Alternatively, you can pass the pointer to RP and simply ignore its value. What you cannot do is try to dereference a NULL pointer as you did in your code.

Comments

0

The following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. only calls the res_calculation() function once
  4. changes the return type for the res_calculation() from float to void and removes the return statement because nothing useful is being returned and main() is not checking for any returned value.
  5. properly checks for I/O errors on the call to scanf() and reports any errors to the user via the stderr output stream.
  6. adds a '\n' to the format parameter of the calls to printf() so the calculated values are immediately passed to the terminal, rather than waiting until after the program exits

and now, the proposed code:

#include <stdio.h>
#include <stdlib.h>

void res_calculation (float r1, float r2, float r3, float* RS, float* RP )
{
    *RS = (r1+r2+r3);
    *RP = (r1*r2*r3)/(r1*r2+r2*r3+r1*r3);
}


int main( void )
{
    float r1,r2,r3,rs,rp;
    printf("Please enter the values for r1, r2 and r3 seperated by a space");
    if( scanf ("%f %f %f", &r1, &r2, &r3) != 3 )
    {
        fprintf( stderr, "scanf for three float values failed\n" );
        exit( EXIT_FAILURE );
    }
    
    res_calculation( r1, r2, r3, &rs, &rp );
    printf("RS: %f\n",rs);
    printf("RP: %f\n",rp);
      
    return 0;
}

1 Comment

Your a legend cant stress enough how much this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.