Retrieving struct from array of struct in C for qsort

Retrieving struct from array of struct in C for qsort

By : user2948620
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:
code :
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;

Share : facebook icon twitter icon
qsort of struct array not working

qsort of struct array not working

By : Djhay
Date : March 29 2020, 07:55 AM
help you fix your problem I am trying to sort a struct run array called results by a char, but when I print the array, nothing is sorted. Have a look at this:
code :
int count_number_of_different_persons(run results[])
run *run1 = *(run **)a;
run *run1 = (run*)a;
How to qsort an array of struct

How to qsort an array of struct

By : SQL database guy
Date : March 29 2020, 07:55 AM
To fix this issue So I was tasked with grabbing a binary file, reading it into an array composed of structs then sorting it based on an array within the struct. The part that I'm stuck on is the sorting. I'm not sure if I'm supposed to sort it as an array (since the binary file is now an array) or sort it as a struct. Heres part of my code bellow. , First off, the comparator function should have the signature:
code :
int compare(const void *v1, const void *v2);
    const FLIGHT *ia = (FLIGHT *)v1;
    const FLIGHT *ib = (FLIGHT *)v2;
    int rc;

    if ((rc = strcmp(ia->OriginCode, ib->OriginCode)) != 0)
        return rc;
    if ((rc = strcmp(ia->DestinationCode, ib->DestinationCode)) != 0)
        return rc;
    return 0;  // Only if the two entries compare as identical
C/C++ qsort an array of struct within a struct

C/C++ qsort an array of struct within a struct

By : Bigozz
Date : March 29 2020, 07:55 AM
around this issue I solved: The problem was the algorithm and the fields of struct edge were not pointers:
Changed that:
code :
 typedef struct EDGE {
    NODE source;
    NODE destination;
    int weight;
typedef struct EDGE {
    NODE *source;
    NODE *destination;
    int weight;
for (int e = 0; e < E; e++) {
    if (findSet(graph->edge[e].source)->data != findSet(graph->edge[e].destination)->data) {
        result[resultE++] = graph->edge[e];
qsort an array of structs in a struct

qsort an array of structs in a struct

By : Và tôi yêu em
Date : March 29 2020, 07:55 AM
I wish this help you I'm attempting to sort an array of structs in a struct based on an int value. I've successfully sorted an array of structs but I'm guessing I'm passing a wrong value somewhere for the nested structs. , There are two mistakes with your code
using qsort on a nested struct array

using qsort on a nested struct array

By : user3718171
Date : March 29 2020, 07:55 AM
I hope this helps you . If you want to sort the elements in the List array using qsort, then do e.g.
code :
qsort(Users[nInd].List, NumberOfElementsInList, sizeof(struct items), CompareItems);
int CompareItems(const void *a, const void *b)
    const struct items *item_a = (const struct items *) a;
    const struct items *item_b = (const struct items *) b;

    if (item_a->nID > item_b->nID)
        return 1;
    else if (item_a->nID < item_b->nID)
        return -1;
        return 0;
if (Users[nInd].List[0].nID > Users[nInd].List[1].nID)
    // Swap the items as index 0 and 1, thereby sorting them
    struct items temp_item = Users[nInd].List[0];
    Users[nInd].List[0] = Users[nInd].List[1];
    Users[nInd].List[1] = temp_item;
Related Posts Related Posts :
  • Program works but outputs trailing garbage values
  • Letting 2 pointer pointing to same address
  • Different ways to print the two-dimensional array's contents
  • C Programming : Confusion between operator precedence
  • C code inside a loop not being executed
  • C - Weird symbols
  • C - Get pointer adress to string
  • how to start a function using a Struct?
  • Trying to tweak sscanf() to ignore \n and \t
  • How to find the inverse of a Rectangular Matrix in C using GSL
  • sizeof() showing different output
  • How to select/read/copy values after specific character in a string
  • Jump from bootloader generates exception
  • Array dropping values, picks up garbage
  • Swig: Syntax error in input(3)
  • multiple definition and making sure function is correctly written
  • MD4 openssl core dumped
  • Undefined-Behavior at its best, is it -boundary break? -bad pointer arithmetic? Or just -ignore of aliasing?
  • Why am i getting problem3.c:20:23: error: expected expression before ‘int’?
  • Right Justified Zero filled String in C
  • C Function with parameter without type indicator still works?
  • How to transmit data from an interrupt handler to an user application?
  • Why do I get the error "bash: ./a.out: Permission denied" when I execute a C program in Linux mint 15
  • syntax of sigchld and its declaration
  • error using g_idle_add() in C++, same thing works in C
  • why if else or nested if else are called single statement in C
  • How do I interpret this printf in C
  • load the functions of a shell script without executing it
  • Is FilterSendNetBufferLists handler a must for an NDIS filter to use NdisFSendNetBufferLists?
  • How to write to flash memory using inline assembly?
  • More Return Statements vs. More Indentation
  • How to show an image on a PictureBox from resource?
  • Having malloced some memory,I could't calculator the proper size of the memories I malloced.I don't know why
  • What is the main difference between integer pointer and character pointer?
  • Why are some functions declared extern and header file not included in source in Git source code?
  • what is the use of fflush(stdin) in c programming
  • Is it safe to return file File descriptor locally allocated from another function In C
  • Changing undefined values of an array
  • What does an empty parameter list mean?
  • using strtol on a string literal causing segmentation fault
  • Same structure objects memory overlap?
  • C-Linux-Any way to pass command "history" to Linux shell?
  • Using #define in defining string size C
  • How to use thread pool and message queues in Multithreaded Matrix Multiplication?
  • Can't find how to select path to run a C program
  • Automatic variable in C not initialized but given fixed value within loop
  • main() function defined without return type gives warning
  • Output of following code with integer, float, char variable
  • why buffer memory allocation error in opencl
  • Why am I getting this error during run-time?
  • Strange behaviour of the pow function
  • task in increment , decrement , printf() , why these are evaluated in this manner in C
  • 28 extra bytes in bss
  • Waiting for multiple events without polling
  • Why are my variables reporting as "undeclared identifier" when compiling?
  • Correct AddNode function but somehing happens when I printf
  • When I traverse in the splay tree, then now which one is root?
  • Data type conversion in Postfix evaluation
  • No output in terminal (Head First C)
  • Data writes over after realloc
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org