Why do we need to add a '\0' (null) at the

2019-01-14 13:45发布

Why do we need to add a '\0' (null) at the end of a character array in C? I've read it in K&R 2 (1.9 Character Array). The code in the book to find the longest string is as follows :

#include <stdio.h>
#define MAXLINE 1000
int readline(char line[], int maxline);
void copy(char to[], char from[]);

main() {
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];
    max = 0;
    while ((len = readline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0)
        printf("%s", longest);
    return 0;
}

int readline(char s[],int lim) {
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0'; //WHY DO WE DO THIS???
    return i;
}

void copy(char to[], char from[]) {
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

My Question is why do we set the last element of the character array as '\0'? The program works fine without it... Please help me...

8条回答
姐就是有狂的资本
2楼-- · 2019-01-14 13:56

It is string terminating symbol,When this is encountered ,compiler comes to know that your string is ended.

查看更多
Luminary・发光体
3楼-- · 2019-01-14 14:00

You have actually written the answer yourself right here:

void copy(char to[], char from[]) {
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

The loop in this function will continue until it encounters a '\0' in the array from. Without a terminating zero the loop will continure an unknown number of steps, until it encounters a zero or an invalid memory region.

查看更多
干净又极端
4楼-- · 2019-01-14 14:01

Especially string pointers pointed to array of characters without length known is the only way NULL terminator will determine the length of the string.

Awesome discussion about NULL termination at link

查看更多
一夜七次
5楼-- · 2019-01-14 14:02

Really, you do not need to end a character array by \0. It is the char*, or the C representation of the string that needs to be ended by it.

As for array, you have to add a \0 after its end if you want to transfer it to the string (representer by char*).

On the other hand, you need to have \0 at the end of the array, if you want to address it as char* and plan to use char* functions on it.

查看更多
祖国的老花朵
6楼-- · 2019-01-14 14:04

You need to end C strings with '\0' since this is how the library knows where the string ends (and, in your case, this is what the copy() function expects).

The program works fine without it...

Without it, your program has undefined behaviour. If the program happens to do what you expect it to do, you are just lucky (or, rather, unlucky since in the real world the undefined behaviour will choose to manifest itself in the most inconvenient circumstances).

查看更多
再贱就再见
7楼-- · 2019-01-14 14:06

Because C defines a string as contiguous sequence of characters terminated by and including the first null character.

Basically the authors of C had the choice to define a string as a sequence of characters + the length of string or to use a magic marker to delimit the end of the string.

For more information on the subject I suggest to read this article:

"The Most Expensive One-byte Mistake" by Poul-Henning Kamp http://queue.acm.org/detail.cfm?id=2010365

查看更多
登录 后发表回答