Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe you have at least one problem here:</p> <pre><code>strncpy(Result, ix + (Offset - LEN),LEN); </code></pre> <p>If you look at the <a href="http://linux.die.net/man/3/strncpy" rel="nofollow">documentation for <code>strncpy</code></a>, you will see that it does not automatically null-terminate the copied string if you hit the character limit. Therefore your <code>Result</code> strings are not null-terminated.</p> <p>Try changing to this:</p> <pre><code>strncpy(Result, ix + (Offset - LEN),LEN); Result[LEN] = '\0'; </code></pre> <p>Of course, you still need to provide memory for the Result string. Currently you are passing an uninitialized pointer into <code>GetStr()</code>:</p> <pre><code>char * Str; char * Str2; </code></pre> <p>Since these are fairly small integers you can use statically allocated storage like this:</p> <pre><code>#define MAX_RESULT_LEN 64 /* ... */ char Str[MAX_RESULT_LEN] char Str2[MAX_RESULT_LEN] /* ... */ if (LEN &gt; MAX_RESULT_LEN - 1) { LEN = MAX_RESULT_LEN - 1; } strncpy(Result, ix + (Offset - LEN),LEN); Result[LEN] = '\0'; </code></pre> <p>Finally, there are some issues with your <code>sort()</code> function. If you look at the <a href="http://linux.die.net/man/3/qsort" rel="nofollow">qsort() documentaton</a>, 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 <code>n1 - n2</code> instead of the <code>n1 &lt; n2</code>.</p> <p>I also thought you're sort arguments of type <code>char **</code> 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 <code>char **</code>.</p> <p>So here is the final version:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #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 &gt; 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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload