The "Segmentation fault (core dumped)" error occurs when using the malloc function. I learned that malloc's initialization value is filled with trash memory. Is Segmentation fault error occurring in this part?
#include <stdio.h>
#include <stdlib.h>
int chnum;
char *getstr(void)
{
char *str1 = (char *)malloc(sizeof(char) * chnum);
printf("Write a string: ");
gets(str1);
return str1;
}
int main(void)
{
printf("What is the maximum length of a string? ");
scanf("%d",chnum);
char *set = getstr();
printf("string : %s \n",set);
free(set);
return 0;
}