Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C language character array does not end with \0. How to output with %s
C language character array does not end with \0. How to output with %s
Dynamic memory allocation replicates past compensation \0

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltstring.h & gt

int main()

{

char array[] = {'h ',' e ',' l ',' l ',' o ' };

char * ptr

Int size = sizeof (array)/sizeof (char);

ptr =(char *)malloc(size+ 1);

memcpy(ptr,array,size);

*(ptr+size)= ' \ 0 ';

printf("%s\n ",ptr);

Free of charge (PTR);

Returns 0;

}