Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char **StringArray(char **arr, int size);

int main()
{
    char **Str;
    int n= 5;
    int i;
    Str= (char**)calloc(5,sizeof(char));
    Str[0]= (char*) calloc(10, sizeof(char));
    Str[0]= "f";
    for(i=1; i<n; i++)
    {
        Str[i]= (char*) calloc(10, sizeof(char));
        printf("Char %d: ",i);
        scanf("%s", *(Str+i));
    }

    for(i=0; i<=n-1; i++)
        {
            printf("char no.%d: %s\n",i, *(Str+i));
        }

    StringArray(Str, n);

        printf("________________________________\n");

    for(i=0; i<=n-1; i++)
        {
            printf("char no.%d: %s\n",i, *(Str+i));
        }

}

char **StringArray(char **arr, int size)
{
        int i;
        int sorted;
        char *temp=(char*) calloc(10,sizeof(char));

    for(i=0;i<size-1;i++)
    {
        if(strcmp(arr[i],arr[i+1])> 0)
        {
            strcpy(temp,*(arr[i]);
            strcpy(*(arr[i]),*(arr[i+1]));
            strcpy(*(arr[i+1]),temp);
        }

    }

    return arr;

}


What I have tried:

But Program still prints the unsorted one only by executing codes in the main function, so what is all about?
Posted
Updated 18-Feb-23 3:28am
v2
Comments
Andre Oosthuizen 17-Feb-23 9:44am    
You stated that you are using C++, C# and Python, please be specific as each will have different outcomes.
Also, by stating the program prints only one, what does it print, what is your question about etc. Please use the Improve Question[^] to improve your question so we can better understand what it is you are having problems with.

Quote:
Str= (char**)calloc(5,sizeof(char));

That should be
C
Str= (char**)calloc(5,sizeof(char *));


Might be there are other errors, as well. Anyway the code is not robust against user input.
 
Share this answer
 
Probably because it doesn't compile: you declare StringArray as returning achar** - but the function body does not have a return statement.

If code doesn't compile, then it doesn't generate an EXE file - so what you are running is the previous version of code that did compile!

Additionally, you should free the memory you dynamically allocate in your function or your app will have a memory leak. I also suspect that your look is going round too many times as it runs from 0 to size - 1 but then tried to access an element above that size and thus outside the array.

You might want to rethink why you are using strcpy at all - surely swapping the pointers is a much more efficient way to do this?
 
Share this answer
 
Currently, this is C source code. The other programming languages according to the tag are irrelevant.

There are several errors:
C
// Str = (char**)calloc(5, sizeof(char)); need space for pointer, use n for size!
Str = (char**)calloc(n, sizeof(char*));
if (!Str)  return 0;  // dont forget to check nullpointer!

// scanf("%s", *(Str + i));  
scanf("%s", Str[i]);  // Problem: user could enter more than 10!

// Str[0] = "f";  here you overwrite the pointer!
Str[0][0] = 'f';

for (i = 0; i < n; i++) 
   if (Str[i]) free(Str[i]);  // dont forget to free ram

if (Str) free(Str);          // dont forget to free ram

return 0;  // missing at end of main
C
// strcpy(temp, *(arr[i]);  does not compile
strcpy(temp, arr[i]);
// strcpy(*(arr[i]), *(arr[i + 1]));  warning
strcpy(arr[i], arr[i + 1]);
strcpy(arr[i + 1], temp);
The hint of OriginalGriff instead of strcpy better to swap the pointers would be to consider.

C
// return arr; useless because its already parameter
void StringArray(char** arr, int size);      
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900