You didn't initialize len or c, which is a problem. Helpful is to see that if you allocate len bytes, the index len-1 is always the last index. Try the following



char c='\0';
char *test=NULL;...