Új hozzászólás Aktív témák

  • Lortech

    addikt

    válasz Steve-S #123 üzenetére

    Minek utaztatod meg a stringeket?
    Hiszen csak a char*-okra mutató pointereket kell kicserélni ahhoz, hogy **matrixban kialakuljon a helyes a sorrend:

    if (strcmp(matrix[i], matrix[j]) < 0)
    {
    char *sp = matrix[i];
    matrix[i] = matrix[j];
    matrix[j] = sp;
    }

    De ha mégis úgy kéne, akkor pl :
    #include <stdio.h>
    #include <string.h>

    void rendez(char **matrix)
    {
    int i, j;
    char ideiglenes[20]="";

    for(i = 0; i < 10; i++)
    for(j = 0; j < 10; j++)
    {
    if (strcmp(matrix[i], matrix[j]) < 0)
    {
    strcpy(ideiglenes, matrix[i]);

    char *segedp;
    segedp = (char *)realloc(matrix[i], strlen(matrix[j]) + 1);
    matrix[i] = segedp;
    strcpy(matrix[i], matrix[j]);

    segedp = (char *)realloc(matrix[j], strlen(ideiglenes) + 1);
    matrix[j] = segedp;
    strcpy(matrix[j], ideiglenes);
    }
    }
    printf("\nRendezve: \n\n");
    for(i = 0; i < 10; i++)
    printf("%s\n",matrix[i]);

    }

    int main(void)
    {

    char *automarkak[10];
    automarkak[0]=(char *)malloc(strlen("Mercedes") + 1);strcpy(automarkak[0], "Mercedes");
    automarkak[1]=(char *)malloc(strlen("Ferrari") + 1);strcpy(automarkak[1], "Ferrari");
    automarkak[2]=(char *)malloc(strlen("Audi") + 1);strcpy(automarkak[2], "Audi");
    automarkak[3]=(char *)malloc(strlen("Lada") + 1);strcpy(automarkak[3], "Lada");
    automarkak[4]=(char *)malloc(strlen("Chevrolet") + 1);strcpy(automarkak[4], "Chevrolet");
    automarkak[5]=(char *)malloc(strlen("Ford") + 1);strcpy(automarkak[5], "Ford");
    automarkak[6]=(char *)malloc(strlen("Ferrari") + 1);strcpy(automarkak[6], "Skoda");
    automarkak[7]=(char *)malloc(strlen("Suzuki") + 1);strcpy(automarkak[7], "Suzuki");
    automarkak[8]=(char *)malloc(strlen("Opel") + 1);strcpy(automarkak[8], "Opel");
    automarkak[9]=(char *)malloc(strlen("Chrysler") + 1);strcpy(automarkak[9], "Chrysler");

    rendez(automarkak);
    }

    [ Szerkesztve ]

    Thank you to god for making me an atheist

Új hozzászólás Aktív témák