Using NULL statement in a function parameter

丶灬走出姿态 提交于 2020-12-26 11:31:19

问题


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:


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



回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/65441254/using-null-statement-in-a-function-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!