How to assign a value to a char* using hex notation?

后端 未结 3 1687
轻奢々
轻奢々 2021-02-02 01:47

I usually use pointers in the following manner

    char *ptr = malloc( sizeof(char) * 100 );
    memset( ptr, 0, 100 ) ;
    strncpy( ptr, \"cat\" , 100 - 1 );
3条回答
  •  别跟我提以往
    2021-02-02 02:27

    strncpy( ptr, "\x63\x61" , 100 - 1 );
    

    0x63 is an integer hexadecimal literal; The C compiler parses it as such within code. But within a string it is interpreted as a sequence of characters 0,x,6,3. The literal for the char with value 63 hex. is '\x63', and within strings you must use this notation. "c\x63" is the literal for a zero-terminated string, irrespective of the characters within the quotes (or of the notation by which you denote them), so no, you don't need to append a trailing zero manually.

提交回复
热议问题