指向结构体数组的指针
我正在尝试比较结构数组的两个节点之间的变量 dist 。
这是结构:
struct arco{
int u, v;
double temp, dist;
}arcos[MAXOASIS];
这是我的代码:
int compDist(const void *a, *void const *b, i, j){
struct arco *ia = (struct arco *)a;
struct arco *ib = (struct arco *)b;
ia->arco[i].dist;
ib->arco[j].dist;
return(if(*ia > *ib)? *ia : *ib)
}
但它是错误的。应该怎么做呢?
I'm trying to compare the variable dist between two nodes of my array of structures.
This is the structure:
struct arco{
int u, v;
double temp, dist;
}arcos[MAXOASIS];
This is my code:
int compDist(const void *a, *void const *b, i, j){
struct arco *ia = (struct arco *)a;
struct arco *ib = (struct arco *)b;
ia->arco[i].dist;
ib->arco[j].dist;
return(if(*ia > *ib)? *ia : *ib)
}
But it is wrong. How should it be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那应该做什么?您可能想要类似的东西
您可以使用一些中间变量来使其更干净。
编辑
根据您的编辑,您可能想要:
What is that supposed to do ? You probably want something like
You could use some intermediate variables to make it cleaner.
EDIT
In light of your edit it's likely you want:
您正在将指针与 strutc 进行比较,而不是与变量进行比较。我不知道你的结构定义,但我认为你需要类似的东西:
You are compairing pointer to strutcs rather then variables. I don't know your struct definition but I think you need something like:
你的代码中有很多错误,
您最初返回一个结构指针,而您的函数声明显示为 int。
另外,像这样的比较
是无效的,你不能只比较两个结构,在c++中,你可以重载“>”运算符,但据我所知,你不能在 c 中做同样的事情。
这应该可行,但我不能保证它,因为我不知道你如何定义你的结构arco。
There are a lots of mistakes in your code,
You were initially returning a structure pointer whereas your function declaration says int.
Also a comparison like
is invalid, you can't just compare two structures, in c++, you could overload the '>' operator but you can't do the same in c as far as i know.
That should work but i can't guarantee it as i have no idea how you've defined your structure arco.
阿科不是阿科的成员。
不要说“这是错误的”,而是发布您遇到的编译器错误,这样您就会得到更好的答案。
arco is not a member of arco.
Instead of saying "it is wrong", post the compiler errors you are getting, and you will get better answers in return.