Re:Did Ken, Ritchie and Brian choose wrong with NUL-terminat...
by bobsc123 on Tuesday January 16, 2018 @02:02PM
(#16161093)
Null terminated strings are just application level logic. "strings" are just bytes in memory. There are no strings.
[ Parent
| on HN ]
Re:Did Ken, Ritchie and Brian choose wrong with NUL-terminat...
by callesgg on Tuesday January 16, 2018 @02:40PM
(#16161530)
No, If you quote a string in c "string data here" you get a piece of data with null termination.
Null termination is part of the C language.
Example: char str[]= "1234";
printf ("%s: %lu", str, sizeof(str));
Prints:
1234: 5
Re:Did Ken, Ritchie and Brian choose wrong with NUL-terminat...
by _kst_ on Tuesday January 16, 2018 @03:28PM
(#16162128)
Use "%zu" to print a value of type size_t.
A couple of ugly corner cases:
const char str[] = "abcd\0efgh";
printf("length = %zu, size = %zu, value = \"%s\"\n",
strlen(str), sizeof str, str);
output: length = 4, size = 10, value = "abcd"
And: const char str[4] = "abcd";
printf("length = %zu, size = %zu, value = \"%s\"\n",
strlen(str), sizeof str, str);
This has undefined behavior. (Which is a good reason to let the compiler figure out how big the array has to be. Computers are better at counting things than you are. Let them.)
Re:Did Ken, Ritchie and Brian choose wrong with NUL-terminat...
by roywiggins on Tuesday January 16, 2018 @02:38PM
(#16161517)
> Should the C language represent strings as an address + length tuple or just as the address with a magic character (NUL) marking the end?
This explicitly is a different kind of representation in memory.