Une faute au niveau d'un "=" ?? :s

james hetfield Messages postés 5 Statut Membre -  
fiddy Messages postés 11653 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,
svp maintenant je suis sur un programme qui ordonne des phrases alphabétiquement et il y a une faute que j arrive pas à corriger c'est au niveau du = dans le 1er for ; la ligne ---mot[i]=malloc(strlen(temp)+1); ----

voila mon code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define n 10
void main()
{
char *mot[n],temp[51];
int i,j,fin;
for(i=0;i<n;i++)
{
printf("\nentrez votre phrase %d (max 50) \n",i+1);
gets(temp);
mot[i]=malloc(strlen(temp)+1);
if(mot[i])
strcpy(mot[i],temp);
else
{
printf("momoire insuffisante \n");
exit(-1);
}
}
for(i=n-1;i>0;i=fin)
{
fin=0;
for(j=0;j<i;j++)
{
if(strcmp(mot[j],mot[j+1])>0)
{
fin=j;
strcpy(temp,mot[j]);
strcpy(mot[j],mot[j+1]);
strcpy(mot[j+1],temp);
}
}
}
puts("\nles phrases trie sont : \n");
for(i=0;i<n;i++)
puts(mot[i]);
printf("\n");
}

1 réponse

fiddy Messages postés 11653 Date d'inscription   Statut Contributeur Dernière intervention   1 847
 
Bonjour,

Avec le code donné, il n'y a pas d'erreur au niveau d'égal. Le problème est ailleurs.
Par contre, le code n'est pas très propre (gets, void main, ...).
Le voici un peu arrangé :

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 10

int main(void)
{
   char *mot[N],temp[51];
   int i,j,fin;
   for(i=0;i<N;i++)
   {
      printf("\nentrez votre phrase %d (max 50) \n",i+1);
      fgets(temp,sizeof temp,stdin);
      mot[i]=malloc(strlen(temp)+1);
      if(mot[i])
         strcpy(mot[i],temp);
      else
      {
         printf("momoire insuffisante \n");
         return -1;
      }
   }
   for(i=N-1;i>0;i=fin)
   {
      fin=0;
      for(j=0;j<i;j++)
      {
         if(strcmp(mot[j],mot[j+1])>0)
         {
            fin=j;
            strcpy(temp,mot[j]);
            strcpy(mot[j],mot[j+1]);
            strcpy(mot[j+1],temp);
         }
      }
   }
   puts("\nles phrases trie sont : \n");
   for(i=0;i<N;i++)
      puts(mot[i]);
   printf("\n");
   return 0;
}

Cdlt,
0