如何正确定义char数组
有谁知道应该如何定义 ASF_OBJECT_GUID_TEST
以避免下面标记的行中出现编译错误,或者我可以在哪里找到有关它的更多信息?
#define ASF_OBJECT_GUID_TEST {(char)0x75, (char)0xB2, (char)0x00}
void testFunction(char * testChar)
{
}
int main(int argc, char * argv[])
{
char test[] = ASF_OBJECT_GUID_TEST;
testFunction(test);
testFunction(ASF_OBJECT_GUID_TEST); // <- fails to compile this line of code
return 0;
}
Does anyone know how should ASF_OBJECT_GUID_TEST
be defined to avoid compilation error in the line marked below, or where I can find more information about it?
#define ASF_OBJECT_GUID_TEST {(char)0x75, (char)0xB2, (char)0x00}
void testFunction(char * testChar)
{
}
int main(int argc, char * argv[])
{
char test[] = ASF_OBJECT_GUID_TEST;
testFunction(test);
testFunction(ASF_OBJECT_GUID_TEST); // <- fails to compile this line of code
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是一个初始值设定项列表,本身不是 C89 中的数组(或指针)。
您可以改为使用:
is an initializer list and is not itself an array (or a pointer) in C89.
You instead could use:
您想要转换 ASF_OBJECT_GUID_TEST,即
testFunction((char[])ASF_OBJECT_GUID_TEST)
或
#define ASF_OBJECT_GUID_TEST (char[]){(char)...}
you want to cast the ASF_OBJECT_GUID_TEST, i.e
testFunction((char[])ASF_OBJECT_GUID_TEST)
or
#define ASF_OBJECT_GUID_TEST (char[]){(char)...}
谢谢。这正是我所寻找的。
假设我定义了许多有 8 个字符的字符串文字,其中每 2 个字符实际上构成一个字节的值。有没有办法创建一个宏,通过在每对字符之间添加“\x”来修改这些字符串文字,如下所示?
其中 TEST 为
"\x33\x69\x81\x49"
Thanks. That was exactly what I was looking for.
Supposed I have defined many string literals which have 8 characters, and each 2 of them make actually the value of a byte. Is there a way to create a macro that would modify those string literals by adding a "\x" between every couple of characters like the following?
Where TEST would be
"\x33\x69\x81\x49"