使用指针和结构C 计算两个点之间的距离,分割故障问题
Calculate distance between two points using pointers and structures C++, Segmentation Fault issue
在这里,我有我的代码来计算用户输入点之间的距离,而 calculateDistance
函数应采用两个指针,我觉得我已经设置了正确的设置,但是当我运行时代码我得到此错误:bash: line 12: 25372 Segmentation fault $file.o $args
代码:
struct Point{
float x;
float y;
};
float calculateDistance(struct Point *p1, struct Point *p2){
float *fx, *fy;
*fx = (*p1).x - (*p2).x;
*fy = (*p1).y - (*p2).y;
return sqrt((*fx * *fx) + (*fy * *fy));
}
int main()
{
struct Point *p1, *p2, q, w;
p1 = &q;
p2 = &w;
//float distance;
cout << "Enter coordinate for p1x: " << endl;
cin >> (*p1).x;
cout << "Enter coordinate for p1y: " << endl;
cin >> (*p1).y;
cout << "Enter coordinate for p2x: " << endl;
cin >> (*p2).x;
cout << "Enter coordinate for p2y: " << endl;
cin >> (*p2).y;
//distance = calculateDistance(*p1, *p2);
cout << "Distance between points: " << calculateDistance(p1, p2) << endl;
return 0;
}
一个故障在功能calculateDistance
中。请更改为
float calculateDistance(struct Point *p1, struct Point *p2){
float fx, fy;
fx = (*p1).x - (*p2).x;
fy = (*p1).y - (*p2).y;
return sqrt((fx * fx) + (fy * fy));
}
相关文章:
- 给定两个偶数,求出它们之间所有偶数的平方和
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- 查找字符在两个索引之间出现的次数
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- C++两个源文件之间共享的枚举的静态实例
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 以天C++为单位的两个时间戳之间的差异
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 在C++中查找两个向量之间最相似的值
- 两个有符号数字之间的距离
- Directx 12 :在两个进程之间共享图形内存
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- 是否有一种标准方法来计算两个 asctime() 值之间的天数
- 如何找到两个棋盘平面之间的角度?
- 计算两个迭代器之间的距离时"Vector Iterators Incompatible"
- 使用 Dijkstra 算法跟踪两个节点之间的最短路径
- 无法理解代码背后的逻辑,这是在两个给定数字之间生成素数的优化问题