C语言 如何#define unsigned char* 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15720112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I #define an unsigned char* string?
提问by Dehalion
I have following define in my code
我在我的代码中有以下定义
#define PRODUCTNAME "SomeName"
and I want to send it with a function com_reply(unsigned char* msg, uint16_t lenght).
我想用一个函数发送它com_reply(unsigned char* msg, uint16_t lenght)。
Now I get a warning that my argument differs in signedness. I know what the problem is and also why com_replyuses unsigned char*instead of char*, I just want to know:
现在我收到一个警告,说我的论点在符号上有所不同。我知道问题是什么以及为什么com_reply使用unsigned char*而不是char*,我只想知道:
How can I define my string as an unsigned char*so I can use it throughout my program without getting warnings all over the place.
如何将我的字符串定义为 anunsigned char*以便我可以在整个程序中使用它而不会到处都收到警告。
EDIT:
编辑:
Strictly speaking I have more than one defines here and the main reason is that there is a BANNERdefine which consists of several other defines, like this:
严格来说,我在这里有多个定义,主要原因是有一个BANNER定义包含其他几个定义,如下所示:
#define PRODUCTNAME "SomeName"
#define PRODUCTDATE "2013-03-30"
#define BANNER PRODUCTNAME " (" PRODUCTDATE ")"
Should I create const variables and concatenate them at program start instead of using defines here?
我应该创建 const 变量并在程序开始时连接它们而不是在此处使用定义吗?
回答by thumbmunkeys
This should work:
这应该有效:
#define PRODUCTNAME ((const unsigned char *)"SomeName")

