[英]pointers to an array of structures
I'm trying to compare the variable dist between two nodes of my array of structures.我正在尝试比较结构数组的两个节点之间的变量 dist 。
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?
应该怎么做?
ia->arco[i].dist;
ib->arco[j].dist;
What is that supposed to do?那应该做什么? You probably want something like
你可能想要类似的东西
return(ia->arco[i].dist > ib->arco[j].dist? ia->arco[i].dist : ia->arco[j].dist)
You could use some intermediate variables to make it cleaner.您可以使用一些中间变量来使其更清洁。
EDIT编辑
In light of your edit it's likely you want:根据您的编辑,您可能想要:
return (ia->dist - ib->dist);
You are compairing pointer to strutcs rather then variables.您正在比较指向 strutcs 而不是变量的指针。 I don't know your struct definition but I think you need something like:
我不知道你的结构定义,但我认为你需要类似的东西:
return(if(ia->arco[i].dist > ib->arco[i].dist)? 1 : 0);
There are a lots of mistakes in your code, You were initially returning a structure pointer whereas your function declaration says int.您的代码中有很多错误,您最初返回的是一个结构指针,而您的 function 声明说 int。
struct arco *compDist(const void *a, *void const *b, i, j){
struct arco *ia = (struct arco *)a;
struct arco *ib = (struct arco *)b;
return(if(ia->arco[i].dist > ib->arco[j].dist)? *ia : *ib)
}
Also a comparison like还有一个比较像
if(*ia > *ib)
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.是无效的,你不能只比较两个结构,在 c++ 中,你可以重载 '>' 运算符,但据我所知,你不能在 c 中做同样的事情。
That should work but i can't guarantee it as i have no idea how you've defined your structure arco.这应该可以,但我不能保证,因为我不知道你是如何定义你的结构 arco 的。
arco is not a member of arco. arco 不是 arco 的成员。
Instead of saying "it is wrong", post the compiler errors you are getting, and you will get better answers in return.不要说“这是错误的”,而是发布你得到的编译器错误,你会得到更好的答案作为回报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.