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

后端 未结 3 1686
轻奢々
轻奢々 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:13

    \xXX is the syntax for inserting characters in hex format. so yours would be:

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

    You don't need to put in a \x00 since having quotes automatically null-delimits the string.

提交回复
热议问题