Logo

Programming-Idioms

History of Idiom 46 > diff from v39 to v40

Edit summary for version 40 by Datadog:
[C] zero-fill t using calloc, malloc does not

Version 39

2019-09-26, 16:10:07

Version 40

2019-09-26, 16:15:08

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Illustration

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Illustration
Imports
#include <stdlib.h>
#include <string.h>
Imports
#include <stdlib.h>
#include <string.h>
Code
char *t=malloc(6*sizeof(char));
strncpy(t,s,5);
Code
char *t=(char *)calloc(6,sizeof(char));
strncpy(t,s,5);