あなたが機能MotMiniscule
にポインタmotf
のためのスペースが割り当てられたことがありません:
strcpy(motf,motm);
motf
のアドレスが不確定であるため、これは未定義の動作です。あなたはそれをを指すようにいくつかのスペースを与える必要があります。
motf = malloc(100);
完全なコードは次のようになります。
char* MotMiniscule(char* mot)
{
char motm[100],c,*motf;
int i=0;
strcpy(motm,mot);
c=motm[i];
while(c!='\0')
{
printf("%c \n",c);
motm[i]=tolower(c);
i++;
c=motm[i];
}
motf = malloc(100); // Allocate some memory
strcpy(motf,motm);
printf("%s\n",motf);
return(motf);
}
int main()
{
char *mot="HEllooOoMA",*min;
min=MotMiniscule(mot);
printf("\n le mot est %s:\n",mot);
printf("|| %s ||",min);
free(min); // Don't forget to free dynamically allocated memory
}
ジョン・ボーデで指摘したように、motm
の使用が完全に冗長です。あなたはそれを安全に取り外すことができます。その上、ダイナミック割り当てのサイズは、mod
の長さに依存する必要があります。したがって、洗練されたコードがこれです。
char* MotMiniscule(char* mot)
{
char c, *motf;
int i = 0;
c = mot[0];
motf = malloc(strlen(mot) + 1); // Allocate some memory
while (c != '\0')
{
printf("%c\n", c);
motf[i] = tolower(c);
i++;
c = mot[i];
}
// No need to copy again, but
motf[i] = '\0'; // Remember to terminate it
printf("%s\n", motf);
return(motf);
}
int main()
{
char *mot = "HEllooOoMA", *min;
min = MotMiniscule(mot);
printf("\n le mot est %s:\n", mot);
printf("|| %s ||", min);
free(min); // Remember to free it
}
「モーフ」はどこにポイントしていますか? – iBug
'motf'は初期化されていないポインタです。 –
必要な場合を除き画像を投稿しないでください。テキストとして投稿してください。 – klutt