1 // Example copied from https://en.cppreference.com/w/c/string/byte/memccpy
8 const char src0
[] = "Stars: Altair, Sun, Vega.";
9 const char* src
= strdup(src0
);
10 const char terminal
[] = {':', ' ', ',', '.', '!'};
11 char dest
[sizeof src0
];
14 for (size_t i
= 0; i
!= sizeof terminal
; ++i
)
16 void* to
= memccpy(dest
, src
, terminal
[i
], sizeof dest
);
18 printf("Terminal '%c' (%s):\t\"", terminal
[i
], to
? "found" : "absent");
20 // if `terminal` character was not found - print the whole `dest`
21 to
= to
? to
: dest
+ sizeof dest
;
23 for (char* from
= dest
; from
!= to
; ++from
)
24 putchar(isprint(*from
) ? *from
: alt
);
30 puts("\n" "Separate star names from distances (ly):");
31 const char *star_distance
[] = {
32 "Arcturus : 37", "Vega : 25", "Capella : 43", "Rigel : 860", "Procyon : 11"
35 char *first
= names_only
;
36 char *last
= names_only
+ sizeof names_only
;
38 for (size_t t
= 0; t
!= (sizeof star_distance
) / (sizeof star_distance
[0]); ++t
)
41 first
= memccpy(first
, star_distance
[t
], ' ', last
- first
);
52 puts("Buffer is too small.");