3 ms·
From the C99 draft at https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf : "A string is a co
by eesmith 3y ago
From the C99 draft at https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf :
"A string is a contiguous sequence of characters terminated by and including the first null character. .. The length of a string is the number of bytes preceding the null character"
This means, for example, strlen() must always check for the location of the first null character - there's no advantage to checking the length.
How would this work?
void *x = malloc(8);
...
uint64_t i = 5216694956355289088; // Python: int.from_bytes(b'Hello!\0\0')
memcpy(x, &i, 8);
char *s = x;
puts(s);
Assuming I did it correctly, this should print "Hello!".
When the length get added to the start of the string?