Why do strings in C need to be null terminated?

2020-01-28 04:13发布

Just wondering why this is the case. I'm eager to know more about low level languages, and I'm only into the basics of C and this is already confusing me.

Do languages like PHP automatically null terminate strings as they are being interpreted and / or parsed?

9条回答
够拽才男人
2楼-- · 2020-01-28 04:46

C strings are arrays of chars, and a C array is just a pointer to a memory location, which is the start location of the array. But also the length (or end) of the array must be expressed somehow; in case of strings, a null termination is used. Another alternative would be to somehow carry the length of the string alongside with the memory pointer, or to put the length in the first array location, or whatever. It's just a matter of convention.

Higher level languages like Java or PHP store the size information with the array automatically & transparently, so the user needn't worry about them.

查看更多
时光不老,我们不散
3楼-- · 2020-01-28 04:47

C has no notion of strings by itself. Strings are simply arrays of chars (or wchars for unicode and such).

Due to those facts C has no way to check i.e. the length of the string as there is no "mystring->length", there is no length value set somewhere. The only way to find the end of the string is to iterate over it and check for the \0.

There are string-libraries for C which use structs like

struct string {
    int length;
    char *data;
};

to remove the need for the \0-termination but this is not standard C.

Languages like C++, PHP, Perl, etc have their own internal string libraries which often have a seperate length field that speeds up some string functions and remove the need for the \0.

Some other languages (like Pascal) use a string type that is called (suprisingly) Pascal String, it stores the length in the first byte of the string which is the reason why those strings are limited to a length of 255 characters.

查看更多
欢心
4楼-- · 2020-01-28 04:47

They need to be null terminated so you know how long they are. And yes, they are simply arrays of char.

Higher level languages like PHP may choose to hide the null termination from you or not use it at all - they may maintain a length, for example. C doesn't do it that way because of the overhead involved. High level languages may also not implement strings as an array of char - they could (and some do) implement them as lists of arrays of char, for example.

查看更多
登录 后发表回答