如果 findset
中的条件没有给出正确的比较。它总是显示不相等。
我哪里做错了?
#include<stdio.h>
struct node {
struct node *p;
int rank;
};
void makeset(struct node x) {
x.p = &x;
x.rank = 0;
}
void findset(struct node x) {
if (x.p == &x) {
printf("It worked bro\n");
}
}
int main() {
int nv, ne;
nv = 4;
ne = 5;
for (int i = 0; i < nv; ++i) {
struct node i;
makeset(i);
findset(i);
}
}
最佳答案
void findset(struct node x)
{
if (x.p == &x)
{
x
是传入地址的副本...x 始终是全新的,&x
不会匹配任何以前的地址。
请注意,这不仅适用于结构,而且适用于所有值...
int i = 3;
void f( int j)
{
// &j != &i
// j == i
// j and I have the same value, but different address
}
int main()
{
f(i);
}
关于结构指针地址比较不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703313/