Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:52:51+00:00 2026-06-13T14:52:51+00:00

I am trying to sort an array of pointers by the memory address: #include

  • 0

I am trying to sort an array of pointers by the memory address:

#include <stdio.h>
#include <stdlib.h>

typedef struct flist {
    int size;
    struct flist *blink;
    struct flist *flink;
} *Flist;

int compare(const void *x, const void *y)
{
    Flist a = (Flist)x;
    Flist b = (Flist)y;

    if(a < b)
        return -1;
    else
        return 1;
}

int main()
{
    int a[] = {3, 1, 2, 4, 0};
    Flist b[5];
    int i;

    for(i = 0; i < 5; i++)
        b[a[i]] = (Flist)malloc(12);

    printf("Here is the array before sorting:\n");
    for(i = 0; i < 5; i++)
        printf("%p\n", b[i]);

    qsort(b, 5, sizeof(Flist), compare);

    printf("Here is the array after sorting:\n");
    for(i = 0; i < 5; i++)
        printf("%p\n", b[i]);
}

However, the program has no effect on the order of the addresses:

Here is the array before sorting:
0x759090
0x759030
0x759050
0x759010
0x759070
Here is the array after sorting:
0x759090
0x759030
0x759050
0x759010
0x759070

Any suggestions would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:52:53+00:00Added an answer on June 13, 2026 at 2:52 pm

    You are missing a level of indirection: qsort sends in addresses of elements being sorted, not the elements themselves.

    In your case, you are seeing addresses of the addresses of your Flist elements being passed. You need to dereference the pointers passed in after casting to Flist* (which is a pointer to a pointer):

    int compare(const void *x, const void *y) {
        Flist a = *((Flist*)x);
        Flist b = *((Flist*)y);
    
        if(a < b)
            return -1;
        else
            return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem : Trying to sort an array coming from a typedef struct I created
I am trying to use qsort from STL to sort array of edge: struct
I'm trying to sort an pointer array using pointers instead of indexes but I'm
I am trying to sort char array like this: #include<algorithm> ... char x[] =
I'm trying to sort an array of pointers to structures where the key to
So I'm trying to sort an array of pointers as seen below. The problem
I'm trying to sort a 2d array of pointers using qsort. The only issue
I'm trying to sort an array of structs using selection sort and pointers, but
I am trying to sort array with usort, but it doesn't work, What am
Trying to sort this array of objects according to (1) depth and (2) weight,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.