我刚开始学习 C 语言中的函数指针,并尝试编写程序,但似乎有问题,我不明白是什么?
I have just started learning function pointers in C and and was trying to code a program but there seems to be a problem and I can't understand what?
所以,我试图编写一个程序,要求用户输入图形的点来计算欧几里得距离,并将其用作半径来给出圆的面积。
这是我的代码:
/*You have to take four points(x1,y1,x2,y2) from the user and use it radius to find area of a circle. To find the distance between these points, you will use the Euclidean distance formula.*/
#include <stdio.h>
#include <math.h>
#define PI 3.14
float euclideanDistance(float x1, float x2, float y1, float y2)
{
float ed = 0;
ed = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
return ed;
}
int areaOfCircle(float x1, float y1, float x2, float y2, float(ed)(float x1, float y1, float x2, float y2))
{
return PI * (*ed)(x1, y1, x2, y2) * (*ed)(x1, y1, x2, y2);
//not sure if it's correct or not. No error squiggles.
}
int main()
{
float x1, y1, x2, y2;
float (*fptr)(float, float, float, float);
fptr = euclideanDistance;
printf("Enter the four points x1,y1,x2,y2 to calculate the Euclidean Distance.\n");
printf("x1:");
scanf("%f", &x1);
printf("y1:");
scanf("%f", &y1);
printf("x2:");
scanf("%f", &x2);
printf("y2:");
scanf("%f", &y2);
;
printf("The euclidean distance is %f", fptr(x1, x2, y1, y2));
printf("The area of the circle which has the above mentioned Euclidean Distance as it's radius is: %f", areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))); //error in this printf.
return 0;
}
这里有两个问题。
我不知道如何在 areaOfCircle 中使用函数 euclideanDistance 作为半径,其次是如何在我的主要函数中实现它。
对于第二个问题,在 VS Code 中,它向我显示了错误。
{"message": "argument of type "float" is incompatible with parameter of type "float (*)(float x1, float y1, float x2, float y2)""}
请解释我做错了什么并指导我。
问题是您基本上是重复呼叫 euclideanDistance
。在 main
中,您执行 areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))
,然后在 areaOfCircle
中,您执行 (*ed)(x1, y1, x2, y2)
。要传递函数指针,您只需像传递任何其他类型的指针一样传递它,而不是使用参数调用它。换句话说,将 areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))
更改为 areaOfCircle(x1, x2, y1, y1, fptr)
。
所以,我试图编写一个程序,要求用户输入图形的点来计算欧几里得距离,并将其用作半径来给出圆的面积。
这是我的代码:
/*You have to take four points(x1,y1,x2,y2) from the user and use it radius to find area of a circle. To find the distance between these points, you will use the Euclidean distance formula.*/
#include <stdio.h>
#include <math.h>
#define PI 3.14
float euclideanDistance(float x1, float x2, float y1, float y2)
{
float ed = 0;
ed = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
return ed;
}
int areaOfCircle(float x1, float y1, float x2, float y2, float(ed)(float x1, float y1, float x2, float y2))
{
return PI * (*ed)(x1, y1, x2, y2) * (*ed)(x1, y1, x2, y2);
//not sure if it's correct or not. No error squiggles.
}
int main()
{
float x1, y1, x2, y2;
float (*fptr)(float, float, float, float);
fptr = euclideanDistance;
printf("Enter the four points x1,y1,x2,y2 to calculate the Euclidean Distance.\n");
printf("x1:");
scanf("%f", &x1);
printf("y1:");
scanf("%f", &y1);
printf("x2:");
scanf("%f", &x2);
printf("y2:");
scanf("%f", &y2);
;
printf("The euclidean distance is %f", fptr(x1, x2, y1, y2));
printf("The area of the circle which has the above mentioned Euclidean Distance as it's radius is: %f", areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))); //error in this printf.
return 0;
}
这里有两个问题。
我不知道如何在 areaOfCircle 中使用函数 euclideanDistance 作为半径,其次是如何在我的主要函数中实现它。
对于第二个问题,在 VS Code 中,它向我显示了错误。
{"message": "argument of type "float" is incompatible with parameter of type "float (*)(float x1, float y1, float x2, float y2)""}
请解释我做错了什么并指导我。
问题是您基本上是重复呼叫 euclideanDistance
。在 main
中,您执行 areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))
,然后在 areaOfCircle
中,您执行 (*ed)(x1, y1, x2, y2)
。要传递函数指针,您只需像传递任何其他类型的指针一样传递它,而不是使用参数调用它。换句话说,将 areaOfCircle(x1, x2, y1, y1, fptr(x1, y1, x2, y2))
更改为 areaOfCircle(x1, x2, y1, y1, fptr)
。