简体   繁体   中英

pointers to an array of structures

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?

  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. 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.

 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM