2017-09-22 5 views
0

Je suis assez nouveau pour C et je suis en train de faire un programme de tri de fusion de base. quand je compile ce code (en utilisant GCC)Erreur: initialiseur de tableau doit être une liste d'initialiseur ou large chaîne littérale dans le programme C Fusion Trier

int join(int arrayA[],int aLength,int arrayB[],int bLength) 
{ 
    int array[aLength+bLength]; 
    int uptoA = 0; int uptoB = 0;//the upto variables are used as the index we have reached 
    while ((uptoA < aLength) && (uptoB < bLength)) 
    { 
     if (arrayA[uptoA] < arrayB[uptoB]) 
     { 
      array[uptoA+uptoB] = arrayA[uptoA]; 
      uptoA++; 
     } else { 
      array[uptoA+uptoB] = arrayB[uptoB]; 
      uptoB++; 
     }//else 
    }//while 

    if (uptoA!=aLength)//if A is the array with remaining elements to be added 
    { 
     for (int i = uptoA+uptoB; i < aLength+bLength; i++) 
     { 
      array[i] = arrayA[uptoA]; 
      uptoA++; 
     } 
    } else {//if B is the array with elements to be added 
     for (int i = uptoB+uptoA; i < aLength+bLength; i++) 
     { 
      array[i] = arrayB[uptoB]; 
      uptoB++; 
     }//for 
    }//else 

    return array; 
}//int join 

int merge_sort(int array[],int arrayLength) 
{ 
    if (arrayLength <= 1) 
    { 
     return array; 
    } 
    if (arrayLength == 2) 
    { 

     if (array[0] > array[1]) {return array;}//if it's fine, return the original array 
     int returningArray[2]; returningArray[0] = array[1]; returningArray[1] = array[0]; //just makes an array that is the reverse of the starting array 
     return returningArray; 

    } 

    int aLength = arrayLength/2; 
    int bLength = arrayLength - aLength; 
    //now I will create two arrays with each of the halves of the main array 
    int arrayAunsorted[aLength]; 
    int arrayBunsorted[bLength]; 
    for (int i = 0; i < aLength; i++) 
    { 
     arrayAunsorted[i] = array[i]; 
    } 
    for (int i = aLength; i < arrayLength; i++)//this goes from the end of arrayA to the end of the main array 
    { 
     arrayBunsorted[i] = array[i]; 
    } 

    int arrayA[aLength] = merge_sort(arrayAunsorted,aLength); 
    int arrayB[bLength] = merge_sort(arrayBunsorted,bLength); 
    printf("I can get this far without a segmentation fault\n"); 

    return join(arrayA,aLength,arrayB,bLength); 

} 

Je sais que certaines parties de ce code sont sous forme terrible et mauvais, mais je vais résoudre ce problème une fois que je reçois le programme fonctionne réellement. Je suis très nouveau à C, donc j'espère que ce n'est pas une question stupide.

+0

Il n'y a pas d 'initialiseur de tableau dans ce code. –

+2

@EugeneSh. L'erreur est probablement de 'int ARRAYA [aLength] = merge_sort (arrayAunsorted, aLength);' – Barmar

+0

@Barmar aha .. manqué celui- –

Répondre

1

Ce n'est pas correct:

int arrayA[aLength] = merge_sort(arrayAunsorted,aLength); 

Tout d'abord, vous ne pouvez pas initialiser un tableau en appelant une fonction. Comme le message d'erreur indique, vous ne pouvez initialiser un tableau en utilisant une liste de initalizer, comme:

int arrayA[aLength] = {1, 2, 3}; 

ou une chaîne de caractères comme:

char str[] = "abc"; 

Deuxièmement, merge_sort ne retourne même pas un tableau, il renvoie int. Il n'est pas possible pour une fonction de retourner un tableau en C, car les tableaux ne peuvent pas être assignés.

join est également incorrect. Vous avez déclaré revenir int, mais à la fin de ce médicament:

return array; 

Lorsque vous retournez un tableau, il est converti en un pointeur, il est donc en fait le retour int*, pas int. Mais vous ne pouvez pas retourner un pointeur sur un tableau local, car la mémoire du tableau devient invalide lorsque la fonction retourne.

fonctions de tri devraient modifier un tableau qui est passé pour eux par l'appelant. Soit le tableau en place, soit l'appelant doit fournir deux tableaux: l'un contenant les données d'entrée et l'autre qui doit être rempli avec le résultat.

Je ne vais pas essayer de réécrire tout le code, il est trop cassé et je n'ai pas le temps. Il y a beaucoup de ressources montrant comment implémenter sorte de fusion dans C.