Strings, or arrays of characters in C must be null terminated to know where they end. Why does the same rule not apply to arrays of other types? eg. How does the computer kn
Short answer: because that’s how C language is defined.
Longer answer: C strings are by itself nothing special. They’re a block of memory that contains bytes, just like any other block. But by defining a convention of terminating the string by 0 all functions can agree on ways to handle the strings.
They could have been done in a way that forces you to handle the length separately and always provide the pointer and length to every function. That is cumbersome so it’s better to just use the terminator. It’s also slower in cases like concatenation since first the ending location has to be looked up.
As for why it isn’t used with other types, it sometimes is. And the reason is the same: it was agreed upon as a convention and in the same way as strings. We don’t know how many values there are so we have a sentinel value in the end. It may be null, 0, or some other value. But we can also not do it and provide the number of elements separately.
It’s also often impossible and/or unnecessary to use a sentinel value, for example if we need the whole datatype or know the size of the data. For example, if we have an RGB image how will we define an end value? We need all the values the bytes can have to define the colors so we can’t have a sentinel. We also don’t need one since we know the size of the image.
As for the computer, it doesn’t know anything about the data. It can only handle bytes and words and whatever it’s built to handle. Strings are much higher level and handled entirely in the used language’s library. The processor just moves data around based on what you tell it to do. And for example PC BIOS uses $ as the terminator character when printing strings, not 0.
You usually pass the length of the array along with the pointer. Nothing "knows" the length of anything.
Memory is memory. An array of bytes could have 0x42 0x41 0x44 0x00 in it, which happens to be the string for "BAD" But it could just as well be an integer representing "1145127936" or a float representing "773.0625"
A string in C is a sequence of char
that is null terminated. It's a special case of a char
array.
You can have a char
array that is not null terminated. For example:
char x[] = { 'a', 'b', 'c' };
Arrays in general are represented as a contiguous sequence of the base type in memory. The language itself doesn't keep track of how big an array is, you're expected to do that yourself.
Arrays of characters do not have to be nul-terminated.
char foo[3]="foo"; //not nul-terminated
char bar[]={'b','a','r'}; //not nul-terminated
It's just that string literals are nul-terminated arrays, and that C makes it really easy to make nul-terminated arrays by using string literals as initializers:
char baz[]="baz"; //nul-terminated because "baz" is
Why C does so is a choice the designer(s) made because using a terminator seemed more convenient to them than maintaining character counts next the character array.
But nothing in C forces this preference on you.