[英]What's the meaning of "++" after a union in C?
Here's the code这是代码
int AreSymEquivalent_hkl(const T_SgInfo *SgInfo, int h1, int k1, int l1,
int h2, int k2, int l2)
{
int iList, mh2, mk2, ml2, hm, km, lm;
T_RTMx *lsmx;
mh2 = -h2;
mk2 = -k2;
ml2 = -l2;
/* check list of symmetry operations */
lsmx = SgInfo->ListSeitzMx;
for (iList = 0; iList < SgInfo->nList; iList++, lsmx++)
{
hm = lsmx->s.R[0] * h1 + lsmx->s.R[3] * k1 + lsmx->s.R[6] * l1;
km = lsmx->s.R[1] * h1 + lsmx->s.R[4] * k1 + lsmx->s.R[7] * l1;
lm = lsmx->s.R[2] * h1 + lsmx->s.R[5] * k1 + lsmx->s.R[8] * l1;
if ( h2 == hm && k2 == km && l2 == lm)
return (iList + 1);
else if (mh2 == hm && mk2 == km && ml2 == lm)
return -(iList + 1);
}
return 0;
}
It's a function defined from a quite famous package 'sginfo' in the area of computational crystallography.它是由计算晶体学领域非常著名的包“sginfo”定义的函数。 (You can download from here if you want: https://github.com/rwgk/sginfo/tree/master/sginfo_1_01 ) So i'm sure there's no bug in it. (如果你愿意,你可以从这里下载: https : //github.com/rwgk/sginfo/tree/master/sginfo_1_01 )所以我确定它没有错误。 The question is in the lines问题在行中
lsmx = SgInfo->ListSeitzMx;
for (iList = 0; iList < SgInfo->nList; iList++, lsmx++)
Where "SgInfo" is a big struct I did not put it here, containing "ListSeitzMx" -- an attribute of a union named "T_RTMx" which consists some matrices information.其中“SgInfo”是一个大结构,我没有把它放在这里,包含“ListSeitzMx”——一个名为“T_RTMx”的联合的属性,它包含一些矩阵信息。 The definition is as follows:定义如下:
typedef union
{
struct { int R[9], T[3]; } s;
int a[12];
}
T_RTMx;
That's the part confuses me, "++" after a union.这就是让我困惑的部分,在联合之后是“++”。 What I know is that the value of int before "++" adds to 1, but the union type makes no sense.我所知道的是,“++”之前的 int 值加到 1,但联合类型没有意义。 Or I have made a big mistake of the whole thing?或者我在整件事上犯了一个大错误? That "lsmx" is not a union or something... As a beginner of C, I have tried to write a small test script on this question but the bugs made me crazy.那个“lsmx”不是联合或其他东西......作为C的初学者,我试图针对这个问题编写一个小测试脚本,但错误让我发疯。 So I finally decided to publish this question...所以我终于决定发布这个问题......
lsmx++
is equivalent to相当于
( orig = lsmx, lsmx = lsmx + 1, orig )
In other words,换句话说,
lsmx++
adds one to lsmx
(as if you had done ( lsmx = lsmx + 1
). lsmx++
向lsmx
添加一个(就像你已经完成了一样( lsmx = lsmx + 1
)。
lsmx++
evaluates to the original value of lsmx
, but that is discarded in your code. lsmx++
计算结果为原始值lsmx
,但那是在你的代码丢弃。
lsmx
is a pointer. lsmx
是一个指针。 Adding 1
to a pointer increases the address by the size of the pointed thing ( sizeof(*lsmx)
).给指针加1
会增加指向对象的大小( sizeof(*lsmx)
)的地址。
For example,例如,
T_RTMx foo[5] = ...;
T_RTMx *lsmx = foo; # Same as &( foo[0] ) # Points to foo[0]
lsmx++; # Points to foo[1]
lsmx += 2; # Points to foo[3]
Keep in mind that a[b]
is completely equivalent to *(a+b)
.请记住, a[b]
完全等同于*(a+b)
。
You are actually not adding to the union.你实际上并没有加入工会。 You are adding to the address.您正在添加地址。
Consider a similar situation:考虑一个类似的情况:
struct abc {
int a, b, c;
//Lots more nonsense
};
Now,现在,
struct abc z;
struct abc *a;
a = &z
This tells that a
stores address of struct abc
type.这表明a
存储struct abc
类型的地址。
Now let's revise how pointers work.现在让我们修改指针的工作方式。
Let's suppose z
resides at a location 1000
.假设z
位于位置1000
。 a
stored that value. a
存储该值。 When I do *a
, I refer to memory location 1000
upto sizeof(struct abc)
.当我做*a
,我指的是内存位置1000
到sizeof(struct abc)
。 As a
is a pointer to the type struct abc
, you can do:由于a
是指向struct abc
类型的指针,您可以执行以下操作:
a->c = 2;
or something like that.或类似的东西。
When you say:当你说:
int *b;
It practically can store address of any other type.它实际上可以存储任何其他类型的地址。 If you really like to be adventurous, you can do this:如果你真的喜欢冒险,你可以这样做:
b = &z;
You may or may not receive warnings depending on your compiler.您可能会或可能不会收到警告,具体取决于您的编译器。
When I store &z
, the only thing the compiler does is treat the value at the address of z
as an int
.当我存储&z
,编译器所做的唯一一件事就是将z
的地址处的z
视为int
。 So b
has the value 1000
.所以b
的值为1000
。
Now when you do *b
, you refer to the address 1000
upto sizeof(int)
.现在当你做*b
,你指的是地址1000
到sizeof(int)
。
Concluding this,总结到这,
b++;
This would just do 1000 + sizeof(int)
, and reach to next int
.这只会做1000 + sizeof(int)
,然后到达下一个int
。
a++;
would do 1000 + sizeof(struct abc)
.会做1000 + sizeof(struct abc)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.