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

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

    Note, you only need \ inside the " " string

    char cat[4];
    cat[0] = 0x63;
    cat[1] = 0x61;
    cat[2] = 0x74;
    car[3] = 0x00;
    
    char cat[] = "\x63\x61\x74"; // note the \0 is added for you
    
    char cat[] = { 0x63, 0x61, 0x74, 0x00 };
    

    Are all the same

提交回复
热议问题