Hi

I have a struct

struct s_list
{
    char name[30];
    char lowest[30];
    int int_value;
}LIST[100];

I want to pass the int_value to a the unix function qsort, but how do i pass structs to a function/procedure?


regards

You use the dot operator to refer to a specific member of a struct straigtup and the arrow operator to refer to a specific member of a struct if you have a pointer to struct instead of the struct itself.

List[100] is an array of type s_list. To pass a given int_value from a given s_list object stored in List you need to use the [] operator to get th e given s_list object and then the dot operator to get the int_value.

List[1].int_value is the int_value of the second s_list object in the array of s_list objects called List.

I didnt ask how to refer to a specific member
I asked how do i pass to a proceudre

Please dont be rude when asking help. Such attitude wont fetch you any help. Mr. Lerner here is a senior member so restate your problem statement rather than asking just someone else to do it rudely.

BTW here is wat i think should suffice ur purpose:

typedef struct 
{
    char input;
    int value;
}List;

int compare (const void* source, const void* dest)
{
    List* listSource = (List*) source;
    List* listDest = (List*) dest;
    if ( (listSource->value) > (listDest->value))
        return 1;
    else if ( (listSource->value) < (listDest->value))
        return -1;
    else
        return 0;
}

int main()
{
     List myList[10];
     for (int i = 0; i < 10; ++i)
     {
         myList[9 - i].input = (char)i + 90;
         myList[9 - i].value = i;
     }
     for (int i = 0; i < 10; ++i)
     {
         printf ("\nThe list %d has character = %c and value = %d ", i + 1, myList[i].input, myList[i].value);
     }
     qsort (myList, 10, sizeof (List), compare);
     for (int i = 0; i < 10; ++i)
     {
         printf ("\nThe list %d has character = %c and value = %d ", i + 1, myList[i].input, myList[i].value);
     }
     getchar ();
     return 0;
};

If you dont understand any part just post again.

Hope it helped,
Bye.

I want to pass the int_value to a the unix function qsort

To pass a specific int to a function you just type the identifierm (aka name of the int) for that int in the appropriate spot.

how do i pass structs to a function/procedure

To pass a struct to a function you just type the identifier of the struct in the appropriate spot.

To me, when you type the identifier of a variable you refer to the variable. Given the concepts of reference variables and passing by reference, etc., the concept of refer to may have been confusing. Sorry.

#include <iostream>
using namespace std;

struct s_list
{
    char name[30];
    char lowest[30];
    int int_value;
};

void myfunc(s_list);

int main
{
    s_list LIST[100];  //LIST is an array of type s_list, not a s_list object
    
    //populate LIST here by whatever technique you want;
   
   //display the member int_value of the second element in LIST; which
   //means the same as pass int_value of the second element in LIST
   //to the << method (aka function or operator, if you prefer) of the  
   //ostream object called cout
   cout << LIST[1].int_value;

   //pass the first s_list object in LIST (that is, a struct) to a function 
   //called myfunc.
   myfunc(LIST[0]);
}

void myfunc(s_list s)
{
    cout << s.name << ' ' << s.lowest;
}

@Lerner

Actually this is not what this guy wants.

He wants to pass the structure to the inbuilt qsort fucntion so that it is automatically sorted.

The technique for the above mentioned has already been posted by me.

just an alternate compare. qsort() doesn't care about actual values, just cares about 0, < 0 or > 0.

int compare (const void* source, const void* dest)
{
   return ((List*)source)->value - ((List*)dest)->value;
}

just an alternate compare. qsort() doesn't care about actual values, just cares about 0, < 0 or > 0.

int compare (const void* source, const void* dest)
{
   return ((List*)source)->value - ((List*)dest)->value;
}

Except that that version is buggy.

where is the bug?

Signed integer overflow is undefined behavior.

It compiles ok for me and returns a value similar to strcmp().

C'mon AD -- "it works ok for me?" :cheesy:

I'd've thought you'd've seen one of my posts about this by now.
[edit][thread]32568[/thread] ;)

Actuall I had forgotton about that discussion -- but if subtracting two integers were really that much of a problem then no computer program would work. So I just dismiss it as a non-issue and non-problem. It isn't really worth the effort to even think about -- wasts too many brain cells (and I need all I can get). It might only be a problem in some remote unlikely os that nearly nobody uses.

returning the difference between two integers will work correctly in probably 99.999999% of all operating systems. So who reall cares whether it may or may not result in some obscure problem. I don't stay awake at night thinking about such problems. And neither should anybody else.

but if subtracting two integers were really that much of a problem then no computer program would work. So I just dismiss it as a non-issue and non-problem. It isn't really worth the effort to even think about -- wasts too many brain cells (and I need all I can get). It might only be a problem in some remote unlikely os that nearly nobody uses.

returning the difference between two integers will work correctly in probably 99.999999% of all operating systems. So who reall cares whether it may or may not result in some obscure problem. I don't stay awake at night thinking about such problems. And neither should anybody else.

A better aproach would be to fix bugs -- not defend them.

It'll be wrong with about half of the possible inputs, given a garden variety behavior of undefined behavior. You've just been testing on the wrong half!

I thought you came from a side of programming a little bit closer to the bare wire?

Yes, I will agree there are some cases where my comparison function doesn't work right -- when either or both numbers are negative. Its not due to some exotic bug or problem with integer overflow but an elementry property of math (subtractiing two negatives is really adding them and that will result in a value with the wrong sign returned to qsort). So my quick comparison function does not work for all values. S.O.S.'s function is better afterall .:)

Yes, I will agree there are some cases where my comparison function doesn't work right -- when either or both numbers are negative.

And when many of them are positive too.

Its not due to some exotic bug or problem with integer overflow but an elementry property of math (subtractiing two negatives is really adding them and that will result in a value with the wrong sign returned to qsort).

First, the result is undefined, so all bets are off when you come to...
The result is ___.

It's a bug if it doesn't work as expected, exotic or not.

First, the result is undefined, so all bets are off when you come to...

you mean the result of adding two numbers whos sum will not fit in the integer is undefined. Of course this is not a total disaster -- just use an integer that can hold larger numbers, which you would be forced to do if you were writing a checkbook program for Bill Gate's back account:cheesy: In that case, c and c++ would not be the desirable computer language. Fortran or C# might be better.

I was not being rude, i just informed him that i did not ask that question, and restated my question

Am I supposed to say 'yes that is what i asked', when it was not.

I was not being rude, i just informed him that i did not ask that question, and restated my question Am I supposed to say 'yes that is what i asked', when it was not.

I very well understand that it was not your intension of being rude, thats why i went to the trouble of putting up the solution for you. I dont give a damn about rude people.
But what i said was not some kind of warning but just a reminder that including the word "please" would be more like it.

In case you dont know, the members of this forum are not bound in any way to help the people who post their problems here. Being actally polite and explaining the problem in depth would urge the people who see to posts to help you.

Hope you understand.
Bye.

This question has already been answered. Start a new discussion instead.