When you declare type
as char type[sz]
, that's giving you a local variable. The lifetime of that memory will end when the function returns. Instead, you need to dynamically allocate memory, for example, using malloc
.
char *type = (char *) malloc (sz * sizeof (char));