我正在阅读K&R的《 C编程语言》,在有关结构的部分中,我遇到了以下代码片段:
struct maxpt = { 320, 200 };
和
/* addpoints: add two points */
struct addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
在第一种情况下,似乎正在将值320和200分配给变量maxpt的成员。但是我注意到结构类型的名称丢失了(不是“ struct struct_name maxpt = {320,200}”吗?在第二种情况下,函数返回类型只是“ struct”而不是“ struct name_of_struct”。
我不明白为什么它们不包含结构名称-它怎么知道它要处理的是哪种特殊类型的结构?我感到困惑的是,在以前的代码片段中它们确实包含结构名称,例如在以下函数的返回类型中,它是“结构点”,而不仅仅是“结构”。为什么在某些情况下而不是在其他情况下包含名称?
/* makepoint: make a point from x and y components */
struct point makepoint(int x, int y)
{
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
我的K&R副本包括在第128页上:
struct point maxpt = { 320, 200 };
并在第130页上:
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
这是第二版,第47版,2011年2月。
如果您的K&R副本包含其他内容,我认为这是错误的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句