プログラムは、文字列中の最小のアスキーコードを持つcharを見つけて出力します。私の問題はメッセージにあります:セグメンテーションフォルト(コアダンプされた)。なぜ、どこで起こりますか? ご注意いただきありがとうございます。セグメンテーションフォールト(コアダンプ) - コードを修正するには?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char* str = NULL;
int* mincode = NULL;
int* count = NULL;
char* mincodeChar = NULL;
str = (char *) malloc(50 * sizeof(char));
mincode = (int *) malloc(1 * sizeof(int));
count = (int *) malloc(1 * sizeof(int));
if (NULL == str || NULL == mincode || NULL == count){
printf("Alloc error");
return EXIT_FAILURE;
}
fgets(str, 50, stdin);
printf("your string: ");
puts(str);
*mincode = (int)(str[*count]);
*mincodeChar = *(str + *count);
for (*count = 0; str[*count] != '\0'; (*count)++) {
if((int)str[*count] < (*mincode)) {
(*mincode) = (int)str[*count];
mincodeChar = (str + *count);
printf("%c", *mincodeChar);
}
}
printf("your character: ");
printf("%c", *mincodeChar);
free(str);
free(mincode);
free(count);
return EXIT_SUCCESS;
}
コンパイル時の固定サイズのメモリを動的に割り当てるのはなぜですか?特に、* one * 'int'のためにメモリを割り当てるのはなぜですか?単純な 'int'変数を使わないのはなぜですか? –
)* mincode =(int)(str [* count]); ':' * count'は初期化されていません。 – BLUEPIXY
また、[mallocの結果のキャストに関するこのディスカッション](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)を参照してください。 –