初始化未签名字符的正确方法* [英] Proper Way To Initialize Unsigned Char*
问题描述
初始化 unsigned char *
的正确方法是什么?我目前正在这样做:
What is the proper way to initialize unsigned char*
? I am currently doing this:
unsigned char* tempBuffer;
tempBuffer = "";
还是我应该使用 memset(tempBuffer,0,sizeof(tempBuffer));
?
推荐答案
第二种方法将给你留下一个空指针。请注意,您此处并未在缓冲区中声明任何空间,而是在必须在其他位置创建的缓冲区中声明 pointer 。如果将其初始化为
,则将使指针指向具有一个字节正好为null的静态缓冲区。如果需要缓冲区,可以稍后再写入字符,请使用Fred的数组建议或类似 malloc
的东西。
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
.
这篇关于初始化未签名字符的正确方法*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!