how does this work

This is a discussion on how does this work within the C Programming forums, part of the General Programming Boards category; how is this function working a mate emailed it to me i understand a bit but really want to know ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    4

    Question how does this work

    how is this function working a mate emailed it to me i understand a bit but really want to know a bit more

    int sort_id(const void *v1, const void *v2) {

    SLIST *a1=(SLIST *)v1;

    SLIST *a2=(SLIST *)v2;



    return (a1->id - a2->id);

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Q??!
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Sayeh
    Guest
    In essence, a1 makes a copy of the v1 pointer. and a2 makes a copy of the v2 pointer. The final line then subtracts a subfield 'id' in the structure pointed at by a2 from the same subfield pointed at by a1.

    The idea of copying the pointers is to work with copies so the originals are never tampered with.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > The idea of copying the pointers is to work with copies so the
    > originals are never tampered with.

    However, in this case, there really is no need to do so:
    Code:
    int sort_id(const void *v1, const void *v2)
    { 
        return ( ((SLIST*)v1)->id - ((SLIST*)v2)->id );
    }
    Either way is fine. You're not doing anything to the pointers. That said, in either case, you could end up changing the contents that are pointed to. The only benifit would be if you were doing something like finding the offset in a string and you needed to preserve the original location that 'v1' was pointing to to do a comparison or something later.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21