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 6543655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:22:21+00:00 2026-05-25T11:22:21+00:00

I need to sort an array of characters in order to iterate over it

  • 0

I need to sort an array of characters in order to iterate over it and print out the unique data points and their count. This array is held inside a linked list node, and I want to use qsort to do this. Unfortunately, I’m getting a segfault right at that particular line.

void printArray(Node_ptr node){

    int count=0; //character count

    char *temp= node->attributes; //duplicate attribute array



    char cur; //current char

    char *outputCat= emalloc(150); //concatenate counts to a single string

    outputCat= "Attribute %d counts are: ";



    qsort(&temp, lineCount, sizeof(char), compare); //sort the array

    ... more code
}

I cribbed the compare method from the man qsort page

int compare(const void *a, const void *b){

  return strcmp(*(char * const *) a, *(char * const *) b);

}

In DDD, the qsort line is the one that triggers the segfault. I originally thought it was due to inaccuracies in the parameters, so I put in some debugging printf statements. printf("%s", temp) prints out 1000 characters, which is exactly what linecount should be. Chars are 1 byte each, so no need for sizeof(char) here.

The error report from ddd on that line is

Program received signal SIGSEGV, Segmentation fault.    
0xb7f8c498 in ?? () from /lib/libc.so.6

Is this qsort’s fault, or something else with my code?

  • 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-05-25T11:22:22+00:00Added an answer on May 25, 2026 at 11:22 am

    This

    qsort(&temp, lineCount, sizeof(char), compare);
    

    Should be:

    qsort(temp, lineCount, sizeof(char), compare);
    

    You do not need to pass the address of a pointer!

    qsort‘s first argument is a pointer, so if you pass it a pointer, you do not need to use the address-of operator, else you’re passing a pointer to a pointer, which is not what you want in this case.

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

Sidebar

Related Questions

I need to sort an array containing arrays in ascending numerical order. The data
I need to sort an array that can look like this: $array[4][0] = array('id'
So Ive got a pretty big array of data and need to sort them
I have the following array. I need to sort this array by the nested
I need to sort this array by the subsubkey description ascending. I tried a
I need to sort this array by distance Array ( [0] => Array (
Can someone help me i need to sort this array: $report_fields['client_id'] = $row['client_id']; $report_fields['name']
I need so sort an array with an array inside, something like this: NSMutableArray
I have an array of filenames and need to sort that array by the
I have an array which i need to sort its elements by occurrence then

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.