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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:56:22+00:00 2026-06-15T04:56:22+00:00

Given a two dimensional array decalred below: char * Arr[4] = { {124 -346

  • 0

Given a two dimensional array decalred below:

char * Arr[4] = 
{
    {"124 -346  DATA...."},
    {"39479 -32 MOREDATA...."},
    {"12 -1 DATA2...."},
    {"100 -45 DATA4...."}
};

i’m trying to use qsort() to sort this function according to the SECOND field, meaning the strings would be ordered according to the lowest second value(-1,-32,-45,-346). I know how to make this function if each value were only one digit, but the digits in the program could be arbitrarily long. here is what i have but the program crashes, if there is a more efficient way to sort this data i would love to here it(i know my method can’t be very efficient).

Sort function(which qsort() calls):

inline void GetStr(char *ix, char* Result)  //call to get second number in function
{
    char *spacing;              //iterator to traverse
    spacing = ix;               //iterator = pos of ix
    int LEN = 0;                //length and offset
    int Offset = 0;

    while(*spacing != ' ')      //while not at end of first num
    {
        Offset++;               //offset is more
        spacing++;
    }
    spacing++;                  //go one ahead of the space
    Offset++;

    while(*spacing != ' ')      //while not end of second number
    {
        spacing++;
        Offset++;
        LEN++;                  //length of number
    }
    strncpy(Result, ix + (Offset - LEN),LEN);
}

int sort(const void* a, const void* b)
{
    char *ia = *(char**)a;
    char *ib = *(char**)b;

    char * Str;
    char * Str2;
    GetStr(ia, Str);                                    //getting some strange errors....... program just crashes
    GetStr(ib, Str2);
    printf("Str: %s Str2: %s", Str, Str2);
    int n1 = atoi(Str);
    int n2 = atoi(Str2);
    return (n1 > n2);
}
  • 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-15T04:56:24+00:00Added an answer on June 15, 2026 at 4:56 am

    I believe you have at least one problem here:

    strncpy(Result, ix + (Offset - LEN),LEN);
    

    If you look at the documentation for strncpy, you will see that it does not automatically null-terminate the copied string if you hit the character limit. Therefore your Result strings are not null-terminated.

    Try changing to this:

    strncpy(Result, ix + (Offset - LEN),LEN);
    Result[LEN] = '\0';
    

    Of course, you still need to provide memory for the Result string. Currently you are passing an uninitialized pointer into GetStr():

    char * Str;
    char * Str2;
    

    Since these are fairly small integers you can use statically allocated storage like this:

    #define MAX_RESULT_LEN 64
    
    /* ... */
    
    char Str[MAX_RESULT_LEN]
    char Str2[MAX_RESULT_LEN]
    
    /* ... */
    
    if (LEN > MAX_RESULT_LEN - 1) {
        LEN = MAX_RESULT_LEN - 1;
    }
    
    strncpy(Result, ix + (Offset - LEN),LEN);
    Result[LEN] = '\0';
    

    Finally, there are some issues with your sort() function. If you look at the qsort() documentaton, you can see that the return value should be “an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second”. The easiest way to achieve this is with the logic n1 - n2 instead of the n1 < n2.

    I also thought you’re sort arguments of type char ** were odd as well, but upon further reflection I realize they are correct. From the qsort docs: “two arguments that point to the objects being compared”. So indeed they will be pointers to C strings or char **.

    So here is the final version:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_RESULT_LEN 64
    
    void GetStr(char *ix, char* Result)  //call to get second number in function
    {
        char *spacing;              //iterator to traverse
        spacing = ix;               //iterator = pos of ix
        int LEN = 0;                //length and offset
        int Offset = 0;
    
        while(*spacing != ' ')      //while not at end of first num
        {
            Offset++;               //offset is more
            spacing++;
        }
        spacing++;                  //go one ahead of the space
        Offset++;
    
        while(*spacing != ' ')      //while not end of second number
        {
            spacing++;
            Offset++;
            LEN++;                  //length of number
        }
    
        if (LEN > MAX_RESULT_LEN - 1) {
            LEN = MAX_RESULT_LEN - 1;
        }
    
        strncpy(Result, ix + (Offset - LEN),LEN);
        Result[LEN] = '\0';
    }
    
    int sort(const void* a, const void* b)
    {
        char *ia = *(char **)a;
        char *ib = *(char **)b;
    
        char Str[MAX_RESULT_LEN];
        char Str2[MAX_RESULT_LEN];
    
        GetStr(ia, Str);
        GetStr(ib, Str2);
    
        printf("Str: %s Str2: %s", Str, Str2);
        int n1 = atoi(Str);
        int n2 = atoi(Str2);
        return (n1 - n2);
    }
    
    int main(void) {
        char * Arr[4] = 
        {
            {"124 -346  DATA...."},
            {"39479 -32 MOREDATA...."},
            {"12 -1 DATA2...."},
            {"100 -45 DATA4...."}
        };
    
        qsort(Arr, 4, sizeof(char *), sort);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to to calculate minimum sum in given two dimensional array #include<iostream> #include<limits.h>
Given this two-dimensional array, each element has two indexes – row and column. <?php
Given an array of elements in PHP, I wish to create a new two-dimensional
There is a points given in two dimensional plane and I want to count
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3
I have a two-dimensional array and want to sort it by name. I would
I just wanna ask what best way to work around a Two-Dimensional Array (2
when declaring the two dimensional array int random[height][width]; and then using it in a
I'm not sure about an error I'm getting while accessing a two-dimensional array in
I want to define a two-dimensional array without an initialized length like this: Matrix

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.