Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the working copy of the program:</p> <pre><code>#include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cstring&gt; char** bubble_sort(const char **filenames, int n) { int i; char **new_list; new_list = (char**) malloc(sizeof(*new_list) * n); for (i = 0; i &lt; n; i++) { new_list[i] = (char*) filenames[i]; } printf("Initial list:\n"); for (i = 0; i &lt; n; i++) { printf("%d: %s\n", i, new_list[i]); } int x; int y; printf("List is sorted:\n"); for(x=0; x&lt;n; x++) { for(y=0; y&lt;n-1; y++) { if(strcmp(new_list[y],new_list[y+1])&gt;0) { char *temp = new_list[y+1]; new_list[y+1] = new_list[y]; new_list[y] = temp; } } } for (i = 0; i &lt; n; i++) { printf("%d: %s\n", i, new_list[i]); } return new_list; } int main(){ const char *ar[5]={ "eee", "aaa", "bbb", "ccc", "ddd", }; bubble_sort(ar, 5); return (0); } </code></pre> <p>Still, keep in mind that your programming style resembles more to C than C++ (which is not always a bad thing).</p> <p>If you want to allocate new strings for your array elements, you should change the first for like this:</p> <pre><code>for (i = 0; i &lt; n; i++) { //new_list[i] = (char*) filenames[i]; new_list[i] = (char*) malloc(sizeof(**new_list) * (strlen(filenames[i]) + 1)); strcpy(new_list[i], filenames[i]); } </code></pre> <p>And this is the <strong>C</strong> version (first one was the C++ version). Note that the string array has all its elements newly allocated, and is not using the initial strings from the input parameter.:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; char** bubble_sort(char **filenames, int n) { int i; char **new_list; new_list = malloc(sizeof(*new_list) * n); for (i = 0; i &lt; n; i++) { //new_list[i] = (char*) filenames[i]; new_list[i] = malloc(sizeof(**new_list) * (strlen(filenames[i]) + 1)); strcpy(new_list[i], filenames[i]); } printf("Initial list:\n"); for (i = 0; i &lt; n; i++) { printf("%d: %s\n", i, new_list[i]); } int x; int y; printf("List is sorted:\n"); for(x=0; x&lt;n; x++) { for(y=0; y&lt;n-1; y++) { if(strcmp(new_list[y],new_list[y+1])&gt;0) { char *temp = new_list[y+1]; new_list[y+1] = new_list[y]; new_list[y] = temp; } } } for (i = 0; i &lt; n; i++) { printf("%d: %s\n", i, new_list[i]); } return new_list; } int main(){ char *ar[5]={ "eee", "aaa", "bbb", "ccc", "ddd", }; bubble_sort(ar, 5); return (0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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