C - why only null char array is complete?

Why should the char array be NULL-terminated? Why is there no int array, for example, at the end should have a separator?

+6
source share
6 answers

char[] should not be NUL terminated. This convention is used when you want to use char arrays as strings. You can use char [] for your purposes without any terminators.

+6
source

This is a matter of convenience.

The ISO C standard, section 7.1.1, defines a string as follows:

A string is a continuous sequence of characters terminated by and including the first null character.

There are many possible ways to represent character strings, such as using a counter and an array, or a counter and a pointer. Zero termination is the method chosen for C for string literals and standard library functions associated with strings.

This is convenient because the null character is not actually used for anything else. It cannot be printed; it is not a control character with some specific display behavior, for example, moving the cursor in a certain way.

You can have arrays of almost any type, but the agreement to use a null value to indicate the end of a sequence may not be as convenient for other types. For integer or floating-point types, zero is a valid value that may be required as regular data in an array.

Pointers have a distinguished value that can be used to indicate the end of a sequence: null pointer NULL . And actually it is sometimes used like that. C command line arguments are passed as a sequence of line pointers; the length of this sequence is indicated by the argc value and is labeled with a trailing null pointer. See also the environ pointer and the exec*() functions on Unix-like systems.

(But for some applications, a null pointer may also be a valid value, so it cannot be used as a terminator.)

Character string manipulation is a pretty big part of what C language and the library say, so it makes sense to have an agreement on how to represent character strings. The convention does not apply just as accurately to arrays of other types.

(By the way, it is important to remember that NULL is a macro that expands to a pointer constant of zero. It is incorrect to use the NULL name to denote the null character '\0' . Both, depending on the context, may be represented in source C as constant 0 , but these are completely different things.)

+3
source

You do not need C char[] have a null end, but a string . Since in C a string means a null terminated array of characters and \0 null terminator, it tells the library where the string ends.

Why?

Since initially C as a basic language with low abstraction, does not have string as the data type, a string in C is just a set of char .

To make it more understandable, there is technically no data type of type string , its just a faรงade that is implemented for the accessibility of programmers, which makes it a high-level concept that is often implemented using the basic char data type.

0
source

I used NULL -terminated int , char* , struct , etc. arrays many times, and I saw the same solution in different code. This is quite common :)

But: there is no language directive for NULL terminated strings, at least not in the core of the language, although some standard C libs use it (mainly in <string.h> )

0
source

In C, the concept of a string is represented as an array of- char that ends with NUL. Therefore, not every char array has a NUL end; you can have an array that does not represent a string. You may have a char array with embedded NUL bytes that terminators do not represent (since the array contains arbitrary binary data and is not a string).

Usually you do not see terminators (sentinel values) for other types of arrays, because, in general, what would you like to use? The gatekeeper value must be one that would not be used for other elements of the array. You see sentinel values โ€‹โ€‹for some special cases; for example, it is sometimes wise to use NULL as a reference value for pointer arrays (for example, the argv argument for main() ).

As for why C strings have a NUL end in general (as opposed to Pascal-style strings), the design is chosen by the author of the language. There are tradeoffs for any approach. Also see What is the rationale for null-terminated strings?

0
source

In an int array, an array of elements will never be used together as a string. However, this applies to the char array. Completing an array of characters with a null value, you can use it as a string.

-1
source

Source: https://habr.com/ru/post/958752/


All Articles