WHere does your string point to? Nowhere!
That’s why you have segmentation fault. You have to either allocate variable on stack as array or define it as pointer and later allocate memory using malloc
. When using malloc
, don’t forget to include “stdlib.h”
Either do this:
char str[6];
strcpy(str,"C-DAC");
or
char *str=malloc(sizeof(*str) * 6);
strcpy(str,"C-DAC");
solved why does the following code produce segmentation fault [duplicate]