1

我有一个像这样的结构的typedef

typedef struct { double x, y; } ACVector;

当我在调试器中查看此实例时,我会得到非常奇怪的输出,例如

(lldb) p _translation
(ACVector) $1 = {
  (double) x = -5503.61
  (double) y = -5503.61
  (CLLocationDegrees) latitude = -5503.61
  (CLLocationDegrees) longitude = -1315.67
}

(lldb) p _translation.x
(double) $2 = -5503.61
(lldb) p _translation.y
(double) $2 = -5503.61

如果我将 ACVector 的定义更改为

typedef struct ACVector { double x, y; } ACVector;

并在调试器中做同样的事情我得到了我所期望的

(lldb) p _translation
(ACVector) $1 = {
  (double) x = -5503.61
  (double) y = -1315.67
}

对 typedef 使用匿名结构是合法的

好的,所以更多的代码

_translation 的声明是作为一个实例变量

ACVector    _translation;

我使用这个函数来初始化变量

ACVector ACVectorMake( double x, double y )
{
    ACVector    r;
    r.x = x;
    r.y = y;
    return r;
}

像这样

_translation = ACVectorMake( d[xp[0]].x-s[xp[0]].x,  d[xp[0]].y-s[xp[0]].y );

原本是一个

ACVector ACVectorMake( double x, double y )
{
    return (ACVector){x,y};
}

以及调试器输出中的纬度和经度元素来自哪里,请注意您无法单独访问它们

响应在其他地方定义的 ACVector 的更多信息

我有两个定义

#define ACVectorZero        (ACVector){(double)0.0,(double)0.0}
#define ACVectorUnit        (ACVector){(double)1.0,(double)1.0}

有趣的是,紧随其后的是

#define ACDegreesFromDegreesMinutesSeconds( d, m, s )                       (CLLocationDegrees)(d+m/60.0+s/3600.0)
#define ACLocationFromDegreesMinutesSeconds( yd, ym, ys, xd, xm, xs )       (CLLocationCoordinate2D){ACDegreesFromDegreesMinutesSeconds( xd, xm, xs ), ACDegreesFromDegreesMinutesSeconds( yd, ym, ys )}

这可以解释也许可以解释 ACVector 中纬度和经度的出现

是否搜索了 ACVector 的每次出现,包括在库中,找不到任何其他出现的 ACVector 正在定义

这都是使用Xcode 4.5 Gold Master

4

2 回答 2

0

我敢打赌,您可能会在变量声明中使用struct ACVector _translation而不是。ACVector _translation

请向我们展示更多代码。

于 2012-09-19T12:05:41.317 回答
0

根据

C语言标准n1256

在 6.7.4 函数说明符下

12
The one exception allows the value of a restricted pointer to be carried
 out of the block in which it (or, more
precisely, the ordinary identifier used to designate it) is declared when
that block finishes execution. 

例如,这允许 new_vector 返回一个向量。

typedef struct { int n; float * restrict v; } vector;
vector new_vector(int n)
{
vector t;
t.n = n;
t.v = malloc(n * sizeof (float));
return t;
}

所以是的,现在我们可以说

对 typedef 使用匿名结构是合法的

因此,现在您正在做其他事情,这会为您带来意外行为..

于 2012-09-19T12:17:05.603 回答