logo
down
shadow

Qsort() doesn't work on a struct


Qsort() doesn't work on a struct

Content Index :

Qsort() doesn't work on a struct
Tag : c , By : Hadley
Date : January 12 2021, 08:33 AM

I wish this helpful for you In short: Your comparison function is wrong. Citing a qsort() manual:
return ptr_b->data[2] < ptr_a->data[2];
int myComp (const void *a, const void *b)
{
    const edge_t * ptr_a = (const edge_t *)a;
    const edge_t * ptr_b = (const edge_t *)b;
    if (ptr_a->data[2] < ptr_b->data[2]) return -1;
    if (ptr_a->data[2] > ptr_b->data[2]) return 1;
    return 0;
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

scanf to struct doesnt work


Tag : c , By : Thaweesak Suksuwan
Date : March 29 2020, 07:55 AM
wish helps you I'm having this function and I need to get coordinates to a structure. These are the structures:
scanf("%d %d",pf1->location.x_l,pf1->location.y_l);
scanf("%d %d",&(pf1->location.x_l), &(pf1->location.y_l));

Why qsort from stdlib doesnt work with double values? [C]


Tag : c , By : SA.
Date : March 29 2020, 07:55 AM
should help you out You want to sort doubles but you compare them as ints... Try this comparison function:
int cmpfunc (const void * a, const void * b)
{
  if (*(double*)a > *(double*)b)
    return 1;
  else if (*(double*)a < *(double*)b)
    return -1;
  else
    return 0;  
}

Retrieving struct from array of struct in C for qsort


Tag : c , By : Jack
Date : November 14 2020, 04:51 PM
I wish did fix the issue. The problem is that your typedef defines test as a pointer type, not a plain type. Then test* becomes a double-pointer, i.e. struct test**. When you write first->data1, you are applying -> operator to a pointer to pointer to struct test, which is not a pointer to a struct.
Since test* is a double-pointer, you need to rewrite your comp to dereference it once before obtaining a member, as follows:
int comp (const void *a, const void* b){
    const test* first = (const test*)a;
    const test* second = (const test*)b;
    return (*first)->data1 - (*second)->data1;
}

qsort doesnt change my array order


Tag : c , By : UpperLuck
Date : March 29 2020, 07:55 AM
Any of those help i have an array of structures (Employee): , You need to change this:
qsort(&employeeArray, 2, sizeof(employee_t), compareEmployeesBySalary);
qsort(employeeArray, 2, sizeof(employee_t *), compareEmployeesBySalary);
int compareEmployeesBySalary(const void* a, const void* b){
        employee_t* one = *(employee_t **)a;
        employee_t* two = *(employee_t **)b;

        if (one->salary == two->salary)
            return 0;
        else if (one->salary > two->salary)
            return 1;
        else
            return -1;
}

Scanf in struct containing 'double' array doesnt work?


Tag : c , By : BinaryBoy
Date : March 29 2020, 07:55 AM
should help you out The struct members x and y arrays can hold only one element each. But you are reading 2 elements as input.
In C, array index ranges from 0 to N-1. Your code has undefined behaviour due to out of bounds access.
 struct _point2d
    {
       double x[2]
       double y[2];
    };
Related Posts Related QUESTIONS :
  • Why does global pointers behave erratically in this function?
  • Write a basic half pyramid pattern program
  • Read memory in C
  • void affecting output of the program
  • How to limit scanf while reading input from file
  • Why does the C runtime not call my exit()?
  • Can anyone say why the array is not changed when passed to a function
  • Integer overflow (Smallest file size that will make the integer in program become negative in bits)
  • Array of pointers to strings(base type of pointers)
  • Is there any way to limit integer datatype value range
  • How to print 2-byte unicode characters
  • The execution of open when using O_CREAT
  • Parsing Command Line Arguments for Shell
  • Undefined length of character arrays
  • Do condition formats in if/else if statements have to match in C?
  • Generate ordered passwords for brute forcing in C
  • Problems with pointer into structure
  • Why does this C program print output "10" irrespective of the for loop?
  • taking output 2D array in matrix form
  • sizeof char pointer and pointer to pointer
  • does the following program cause memory leak?
  • Innacurate file readings from fopen and/or fscanf
  • Searching an element in an Array using Recursive Function in C Language
  • Most fastest C code to count recursively directories in Linux ( without files )
  • Why does C not offer syntactically transparent references like C++ and Java do?
  • Code doesn't get excuted after using continue in while loop
  • How can I maintain correlation between structure definitions and their construction / destruction code?
  • Avoid race conditions when using pointers and threads
  • Binary and Decimal converting
  • How to create input tensors and use with interpreter in Tensorflow Lite (experimental C API)?
  • Unexpected typecasting between values in C
  • Trouble with Forking Process and Calling bc using execve
  • Glib Threads vs GMain Loop Eventing
  • Why does the byte sequence turn when I cast a char array to an integer array?
  • Is there any difference usage in external interrupt between GPIO (AHB bus)and those (APB bus)?
  • Trouble programming AVR to interpret input from Arduino rotary encoder module
  • Is it correct to use a do-while loop inside a for loop? Why and why not?
  • Why we can't use dot for new created pointers to structs
  • Atomicity of fprintf from MPI processes
  • Printing of negative value in c via printf
  • What's the difference between global or local variables regarding the main function?
  • movsd from memory to xmm0 in c x86-64 jit
  • Problem with a function that insert the content of a csv into an array of struct
  • Segmentation Fault running time on sem_post(flag)
  • Extracting values from an incoming bluetooth serial on an arduino
  • "How much memory space does an array takes if the maximum size that is declared is not used?"
  • C GTK2 frustrated with gnome documentation
  • What really happens when a dynamic memory allocation is explicitly converted to struct type?
  • Re-Indexing Bits Within a Char
  • pointer de-referencing balagurusamy
  • Is it safe to memcpy regex_t?
  • Find a tight upper bound on complexity of the below program:
  • FFTW results differ from FFT in MATLAB
  • How to fix Misra 2012 violation , " Assignment operation in expression "
  • What is the difference between "int *p =0;" and "int *p; *p=0;"
  • Strncpy gives unwanted characters at end of string
  • What is really happening behind when a constant is assigned to a pointer variable?
  • Having trouble tracking logic of program includes Fork()
  • Are leftshift operators dependent on register size?
  • How to pass and receive back pointer to array of structures?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com