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);
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);
Q??!
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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.
> 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:
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.Code:int sort_id(const void *v1, const void *v2) { return ( ((SLIST*)v1)->id - ((SLIST*)v2)->id ); }
Quzah.
Hope is the first step on the road to disappointment.