Proper Way To Initialize Unsigned Char*

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

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

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

<
7条回答
  •  猫巷女王i
    2021-02-04 06:45

    The second method will leave you with a null pointer. Note that you aren't declaring any space for a buffer here, you're declaring a pointer to a buffer that must be created elsewhere. If you initialize it to "", that will make the pointer point to a static buffer with exactly one byte—the null terminator. If you want a buffer you can write characters into later, use Fred's array suggestion or something like malloc.

提交回复
热议问题