The program finds a char with the smallest ascii code in a string and outputs it. My problem is in message: Segmentation fault(core dumped). Why and Where does it occur? Thanks for attention.
#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;
}