The terminating NULL in an array in C

前端 未结 9 1010
走了就别回头了
走了就别回头了 2021-01-21 04:15

I have a simple question.
Why is it necessary to consider the terminating null in an array of chars (or simply a string) and not in an array of integers. So when i want a

相关标签:
9条回答
  • 2021-01-21 04:31

    It's not about declaring an array that's one-bigger, it's really about how we choose to define strings in C.

    C strings by convention are considered to be a series of characters terminated by a final NUL character, as you know. This is baked into the language in the form of interpreting "string literals", and is adopted by all the standard library functions like strcpy and printf and etc. Everyone agrees that this is how we'll do strings in C, and that character is there to tell those functions where the string stops.

    Looking at your question the other way around, the reason you don't do something similar in your arrays of integers is because you have some other way of knowing how long the array is-- either you pass around a length with it, or it has some assumed size. Strings could work this way in C, or have some other structure to them, but they don't -- the guys at Bell Labs decided that "strings" would be a standard array of characters, but would always have the terminating NUL so you'd know where it ended. (This was a good tradeoff at that time.)

    0 讨论(0)
  • 2021-01-21 04:34

    You don't have to terminate a char array with NULL if you don't want to, but when using them to represent a string, then you need to do it because C uses null-terminated strings to represent its strings. When you use functions that operate on strings (like strlen for string-length or using printf to output a string), then those functions will read through the data until a NULL is encountered. If one isn't present, then you would likely run into buffer overflow or similar access violation/segmentation fault problems.

    In short: that's how C represents string data.

    0 讨论(0)
  • 2021-01-21 04:39

    Null terminators are required at the end of strings (or character arrays) because:

    1. Most standard library string functions expect the null character to be there. It's put there in lieu of passing an explicit string length (though some functions require that instead.)
    2. By design, the NUL character (ASCII 0x00) is used to designate the end of strings. Hence why it's also used as an EOF character when reading from ASCII files or streams.

    Technically, if you're doing your own string manipulation with your own coded functions, you don't need a null terminator; you just need to keep track of how long the string is. But, if you use just about anything standardized, it will expect it.

    0 讨论(0)
  • 2021-01-21 04:41

    The reason is it was a design choice of the original implementors. A null terminated string gives you a way to pass an array into a function and not pass the size. With an integer array you must always pass the size. Ints convention of the language nothing more you could rewrite every string function in c with out using a null terminator but you would allways have to keep track of your array size.

    0 讨论(0)
  • 2021-01-21 04:45

    Because of the the technical reasons of how C Strings are implemented compared to other conventions

    0 讨论(0)
  • 2021-01-21 04:47

    The purpose of null termination in strings is so that the parser knows when to stop iterating through the array of characters.

    So, when you use printf with the %s format character, it's essentially doing this:

    int i = 0;
    while(input[i] != '\0') {
       output(input[i]);
       i++;
    }
    

    This concept is commonly known as a sentinel.

    0 讨论(0)
提交回复
热议问题