Search code examples
c++pointersnullcharunsigned-char

Is NULL (or 0 or '\0') value interpreted differently in unsigned char array and char array in c++?


How is NULL (or 0 or '\0') behaved in unsigned char array and char array? In a char array NULL determines the end of the char array. Is it the same case with an unsigned char array? If not how can we determine the end of an unsigned char array?


Solution

  • The exact definition of NULL is implementation-defined; all that's guaranteed about it is that it's a macro that expands to a null pointer constant. In turn, a null pointer constant is "an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t." It may or may not be convertible to char or unsigned char; it should only really be used with pointers.

    0 is a literal of type int having a value of zero. '\0' is a literal of type char having a value of zero. Either is implicitly convertible to unsigned char, producing a value of zero.

    It is purely a convention that a string in C and C++ is often represented as a sequence of chars that ends at the first zero value. Nothing prevents you from declaring a char array that doesn't follow this convention:

    char embedded_zero[] = {'a', '\0', 'b'};
    

    Of course, a function that expects its argument to follow the convention would stop at the first zero: strlen(embedded_zero) == 1;.

    You can, of course, write a function that takes unsigned char* and follows a similar convention, requiring the caller to indicate the end of the sequence with an element having zero value. Or, you may write a function that takes a second parameter indicating the length of the sequence. You decide which approach better fits your design.