-2

I want to sort arrays with using strcmp . How can do it ?

void sort(char s[100][100], int n){
int i, j, cmp;
char tmp[10];

if (n <= 1)
    return; // Already sorted

for (i = 0; i < n; i++)
{
    for (j = 0; j < n-1; j++)
    {
        cmp = strcmp(&s[i][j],&s[i][j+1]);

        if (cmp > 0)
        {
            strcpy(tmp, &s[i][j+1]);
            strcpy(&s[i][j+1],&s[i][j]);
            strcpy(&s[i][j], tmp);
        }
    }
}}

I call function for an array like this type :

int main(){

char *result[6][6];
int a=0;
    int b=1;
    for(a=0; a<5; a++){
    for(b=1;b<4;b++){
        printf("%s\n", result[a][b]);
        sort(result[a][b],6);
    }

}
}

how can I fixed this. Now, I have one warning

joseph
  • 19
  • 4

1 Answers1

1
  1. Use meaningful names, example length instead of b.

  2. Enable complier warnings: a=sort(result,6); ... int sort(char *a,int b){ should generate a warning - or get a new compiler.

  3. temp=a[i][j]; ... ... =temp; attempts to swap pointers. Code needs to swap the array contents.

  4. char *result[6][6]; is a 6x6 array of pointers. Code more like wants to use char result[6][6]; or char *result[6];

  5. Suggest re-working code to the point that your compiler (with all warnings enabled) no longer complains.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256