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

  • SEARCH
  • Home
  • 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 7783539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:52:07+00:00 2026-06-01T19:52:07+00:00

I’m trying to sort a 2d array of pointers using qsort. The only issue

  • 0

I’m trying to sort a 2d array of pointers using qsort. The only issue I have right now is originally I was using statically declared arrays now switching over to pointers. I’m almost tempted to switch to structs but being stubborn that I can’t get this to work.

So far I malloc the 2d array of pointers[array2d[m][3] was the intended size]:

     int **array2d;

     array2d = (int**)malloc((m)*sizeof(int*));

     for(i=0; i<=m; i++)
       array2d = [i]=(int*)malloc(3*sizeof(int));
     qsort(array2d, m, 3*sizeof(int**),comp);

My compare is:

int comp(const void* left, const void*right)                                                                                  
{

  const int *a = *(const int**)left;
  const int *b = *(const int**)right;

  return a-b;
}

Although I’m not sure how to structure the compare to work with 2d pointers.

  • 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-01T19:52:10+00:00Added an answer on June 1, 2026 at 7:52 pm

    From the code snippet you provided I am assuming you were trying to sort each row of the matrix separately. The first thing I noticed is that there is a typo in the memory allocation of columns (2nd index) of the matrix.

    Correct memory allocation of a numRow x numColumns matrix would be as follows:

    /* loop counter */
    int i;
    
    /* dynamic array sizes */
    const int numRow = 5;
    const int numColumns = 25;
    
    /* allocate the row pointers */
    int **dynamic2d = (int **)malloc(numRow * sizeof(int *));
    
    /* for each row pointer */
    for(i = 0; i < numRow; i++)
    {
        /* allocate columns */
        dynamic2d[i] = (int *)malloc(numColumns * sizeof(int));
    }
    

    Next you won’t be able to simply call the qsort(..) method only once. That method expects a “flat” or one-dimensional array. You will need to call the qsort(…) method separately for each row of the matrix. This is demonstrated below:

    /* sort array */
    for(i = 0; i < numRow; i++)
        qsort(dynamic2d[i], numElements, sizeof(int *), comp);
    

    Lastly, you made a mistake with your comparator method. This method has strict rules that need to be followed in order to work correctly. Current specifications say, “The application shall ensure that the function returns an integer less than, equal to, or greater than 0, if the first argument is considered respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is unspecified.“

    This is a simple fix. Simply write the logic to produce those results as seen below:

    int comp(const void* firstArg, const void* secondArg)
    {
       /* get the values of the arguments */
       int first = *(int *)firstArg;
       int second = *(int *)secondArg;
    
       /* return the value as expected by the qsort() method */
       if(first < second)
       {
          return 1;
       }
       else if(second < first)
       { 
         return -1;
       }
    
       return 0;
    }
    

    Last thing to note, this will sort greatest to least. Do not switch the logic around in the comparator if you want least to greatest. The sort will not return accurate results. The correct way to do this is by reading the array from back to front as seen below: You can swap the arguments in the comparator to change the sorting order or read the results from back to front.

    int comp(const void* firstArg, const void* secondArg)
    {
        /* get the values of the arguments */
        int first = *(int *)secondArg;
        int second = *(int *)firstArg;
        ...
    }
    

    or

    /* print greatest to smallest */
    for(i = 0; i < numRow; i++)
    {
        /* start at front and work to back */
        for(j = 0; j < numColumns; j++)
            printf("%d ", dynamic2d[i][j]);
        printf("\n");
    }
    
    /* print smallest to greatest */
    for(i = 0; i < numRow; i++)
    {
        /* start at back and work to front */
        for(j = numColumns- 1; j >= 0; j--)
            printf("%d ", dynamic2d[i][j]);
        printf("\n");
    }
    

    Hopefully this helps! If you need to sort the entire matrix as a whole… that is a different beast all together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.