문자열에서 가장 작은 ASCII 코드를 가진 char을 찾고 출력합니다. 내 문제는 메시지에 있습니다 : 분할 오류 (코어 덤프). 왜 그리고 어디에서 발생합니까? 감사합니다.분할 오류 (코어 덤프) - 내 코드를 수정하는 방법?
NULL 포인터 역 참조#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' 변수를 사용하지 않을까요? –
1) * mincode = (int) (str [* count]);':'* count'는 초기화되지 않습니다. – BLUEPIXY
[malloc의 결과 캐스팅에 대한이 토론] (http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)도 참조하십시오. –