在以下代码中:
typedef struct{int data1; int data2} node;
node n1;
node* n2;
sizeof(n1) returns 8 // size of the struct node
sizeof(n2) returns 4 // since n2 is a pointer it returns the size of the pointer
sizeof(*n2) returns 8 // HOW DOES THIS WORK ?
Run Code Online (Sandbox Code Playgroud)
sizeof实际上如何工作?在上面的例子中,*n2归结为提供n2指向的地址.在这种情况下,n2仍然是一个悬空指针,因为我们既没有分配内存,也没有将它指向某个有效地址.它如何正确地给出结构的大小?
我需要计算两点之间的距离(给定纬度和经度)。我在 C# 中实现了标准半正矢公式
private double toRadian(double val)
{
return (Math.PI / 180) *
}
public double Distance(Position pos1, Position pos2,DistanceType type)
{
double R = (type == DistanceType.Miles) ? 3960 : 6378137; // 6318137 in meters
double dLat = toRadian(pos2.Latitude - pos1.Latitude);
double dLon = toRadian(pos2.Longitude - pos2.Longitude);
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(this.toRadian(pos1.Latitude)) * Math.Cos(this.toRadian(pos2.Latitude)) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double …Run Code Online (Sandbox Code Playgroud)