Proper Way To Initialize Unsigned Char*

前端 未结 7 2030
渐次进展
渐次进展 2021-02-04 06:03

What is the proper way to initialize unsigned char*? I am currently doing this:

unsigned char* tempBuffer;
tempBuffer = \"\";

<
7条回答
  •  醉话见心
    2021-02-04 06:45

    As it's a pointer, you either want to initialize it to NULL first like this:

    unsigned char* tempBuffer = NULL;
    unsigned char* tempBuffer = 0;
    

    or assign an address of a variable, like so:

    unsigned char c = 'c';
    
    unsigned char* tempBuffer = &c;
    

    EDIT: If you wish to assign a string, this can be done as follows:

    unsigned char myString [] = "This is my string";
    unsigned char* tmpBuffer = &myString[0];
    

提交回复
热议问题