How to print a string with embedded nulls so that “(null)” is substituted for '\0'

后端 未结 4 489
别那么骄傲
别那么骄傲 2021-01-21 15:51

I have a string I composed using memcpy() that (when expanded) looks like this:

char* str = \"AAAA\\x00\\x00\\x00...\\x11\\x11\\x11\\x11\\x00\\x00...\";
<         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 16:31

    You have to do that mapping yourself. If you want to, that is. In C, strings are null-terminated. So, if you use a formatted output function such as printf or puts and ask it to print a string (via the format specifier %s) it'd stop printing str as soon as it hits the first null. There is no null word in C. If you know exactly how many characters you have in str you might as well loop over them and print the characters out individually, substituting the 0 by your chosen mnemonic.

    The draft says 7.21.6.1/8:

    p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

    However, the following:

    $ cat null.c
    #include 
    int main() {
     printf("%p\n", (void *)0);
    }
    

    produces:

    00000000
    

    on both gcc 4.6 and clang 3.2.

    However, on digging deeper:

    $ cat null.c
    #include 
    int main() {
     printf("%s\n", (void *)0);
    }
    

    does indeed produce the desired output:

    (null)
    

    on both gcc and clang.

    Note that the standard does not mandate this:

    s If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type.280) Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.

    Relying on this behavior may lead to surprises!

提交回复
热议问题