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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:07:00+00:00 2026-05-29T16:07:00+00:00

This program so far has one purpose, take in two integers(the size of the

  • 0

This program so far has one purpose, take in two integers(the size of the user defined arrays) and then takes in one element or character at a time and adds them to the array. Once both arrays are filled, one of the arrays must be alphabetized (I’m attempting to do this with the built in ‘qsort’).

However, this code hits a runtime error once the qsort is called, and my problem is I don’t know why or how to fix it.

My Code:

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


int toExit(int exit){
    while(exit != 1 || exit !=2){

        printf("\n\nPlease Choose:\n1) Exit\n2) Run program again\nYour choice: ");
        scanf("%d", &exit);
        printf("\n");

        switch(exit){
        case 1:
            return exit;
        case 2:
            return exit;
        default:
            printf("That is not one of the given options.\n\n");
        }
    }

}    

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

    const char* a1 = *(const char**)a;
    const char* b1 = *(const char**)b;
    return strcmp(a1,b1);
}  

int main(void) {

    int exit=0, i, j;
    int lengthX, lengthA;
    char *Xsequence, *Asequence, *_$;

    while(exit != 1){


    printf("please enter the length of sequence A: ");
    scanf("%d", &lengthA);
    printf("please enter the length of sequence X: ");
    scanf("%d", &lengthX);

    printf("\n");  //spacing, visual look of the program

    Asequence =(char*) malloc(lengthA*sizeof(char));
    Xsequence =(char*) malloc(lengthX*sizeof(char));

    for(j=0;j<=lengthA-1;j++)
    {
        printf("Element %d of A: ",j+1);
        scanf("%s", &Asequence[j]);
    }

    printf("Last Element of A (looking for \"$\"): ");
    scanf("%s", &_$);
    printf("\n");  //spacing, visual look of the program

    for(j=0;j<=lengthX-1;j++)
    {
        printf("Element %d of X: ",j+1);
        scanf("%s", &Xsequence[j]);
    }

    printf("Last Element of X (looking for \"$\"): ");
    scanf("%s", &_$);
    printf("\n");  //spacing, visual look of the program

    qsort (Xsequence, lengthX, sizeof(char*), compare);
    printf("The \"A\" sequence is: %s\n",Asequence);
    printf("The \"X\" sequence is: %s\n",Xsequence);


    exit = toExit(exit);

    }
// return 0; 
}
  • 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-29T16:07:01+00:00Added an answer on May 29, 2026 at 4:07 pm

    XSequence is just a char-pointer, so you need only a straight cast:

    const char * a1 = (const char *) a;
    const char * b1 = (const char *) b;
    

    Or simply:

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

    Your code would work if you passed it &XSequence, but there’s no need for the extra indirection.

    You also need to pass sizeof(char), or just 1, as the element size. You are not sorting pointers, but actual characters!

    (I think this is classical case of “overthinking it”. Your actual scenario is a classic, on-the-nose use case for qsort, and you made it way more complicated than it is.)

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

Sidebar

Related Questions

Take a guess ... how long will this program take to produce the very
My company maintains two SVN repositories, Repository 1 and Repository 2: This repository has
I am converting a Fortran program to C#. This has to be done in
I'm using Delphi 2009. My program has been compiling and running fine. I use
I hope this isn't too vague, but I'm stuck on a problem that has
I have 2 one dimensional arrays, containing the exact same values: public int[] start
Introduction Hello folks, I recently learned to program in C! (This was a huge
So far in development, my application has been storing its databases in the app
I have a program that I am trying to take a set of numbers
I am working on a program which includes a general purpose engine, some program

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.