2 ms·
Another issue with C being an allegedly good beginner language is strings. C is simply terrible at handling strings. Let's say I want to concatenate two strings
by WalterBright 5y ago
Another issue with C being an allegedly good beginner language is strings. C is simply terrible at handling strings. Let's say I want to concatenate two strings:
char *concat(char *p, char *q)
{
size_t plen = strlen(p);
size_t qlen = strlen(q);
char *r = (char*)malloc(plen + qlen + 1);
assert(r);
memcpy(r, p, plen);
memcpy(r + plen, q, qlen + 1);
return r;
}
And never mind the issue of when to free() r. Contrast with D:
string concat(string p, string q)
{
return p ~ q;
}
P.S. Yes, I know about the potential overflow problem with `plen + qlen + 1`.
- ziml77 5y agoI get that C tries to be incredibly barebones, but operations on strings are so common that it's crazy how bad the string handling in C is. It seems like it was designed under the assumption that all of your strings are stored in fixed-size buffers. I might not pick D as a beginner language simply because it unfortunately isn't very popular, but I certainly wouldn't pick C as a beginner language.