Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - why is only char array null terminated? [closed]

Tags:

c

string

Why does a char array needs to be NULL-terminated? Why does not an int array for example needs to have a delimiter at the end?

like image 486
Iceman Avatar asked Nov 25 '13 04:11

Iceman


2 Answers

char[] doesn't have to be NUL terminated. It's a convention used when you want to use char arrays as strings. You can use a char[] for your own purposes without any terminator.

like image 52
John3136 Avatar answered Oct 05 '22 12:10

John3136


It's a matter of convenience.

The ISO C standard, section 7.1.1, defines a string this way:

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

There are a number of possible ways to represent character strings, such as using a count and an array, or a count and a pointer. Null termination happens to be the way chosen for C, for string literals and the standard library functions that deal with strings.

It's convenient because the null character is not really used for anything else. It's not printable, it's not a control character with some defined display behavior like moving the cursor in some particular way.

You can have arrays of just about anything, but the convention of using a zero value to mark the end of a sequence happens not to be as convenient for other types. For integer or floating-point types, zero is a valid value that you might want to have as normal data in an array.

Pointers do have a distinguished value that can be used to mark the end of a sequence: the null pointer NULL. And in fact it's sometimes used that way. A C program's command-line arguments are passed as a sequence of pointers to strings; the length of that sequence is indicated by the value of argc and marked by a terminating null pointer. See also the environ pointer and the exec*() functions on Unix-like systems.

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

Character string manipulation is a fairly big part of what the C language and library are about, so it makes sense to have a convention for how to represent character strings. The convention does not apply as neatly to arrays of other types.

(Incidentally, it's important remember that NULL is a macro that expands to a null pointer constant. It's incorrect to use the name NULL to refer to the null character '\0'. Both, depending on the context, can be represented in C source as the constant 0, but they're quite different things.)

like image 26
Keith Thompson Avatar answered Oct 05 '22 13:10

Keith Thompson