问题
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:
- cleanly compiles
- performs the desired functionality
- only calls the
res_calculation()
function once - changes the return type for the
res_calculation()
fromfloat
tovoid
and removes thereturn
statement because nothing useful is being returned andmain()
is not checking for any returned value. - properly checks for I/O errors on the call to
scanf()
and reports any errors to the user via thestderr
output stream. - 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