在编译期确定#define的字符串长度 [英] Determine #defined string length at compile time
问题描述
我有一个 C-活动(一Apache模块,即程序运行频率),这是会的write()
通过套接字0结尾的字符串,所以我需要知道它的长度。
I have a C-program (an Apache module, i.e. the program runs often), which is going to write()
a 0-terminated string over a socket, so I need to know its length.
该字符串已经#defined为:
The string is #defined as:
#define POLICY "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE cross-domain-policy SYSTEM\n" \
"\"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" \
"<cross-domain-policy>\n" \
"<allow-access-from domain=\"*\" to-ports=\"8080\"/>\n" \
"</cross-domain-policy>\0"
有没有讨好的方式,比用 strlen的(政策)+1
在运行时(并因此一再长度计算)更好呢?
Is there please a way, better than using strlen(POLICY)+1
at the runtime (and thus calculating the length again and again)?
一个preprocessor指令,这将允许设置 POLICY_LENGTH
已经在编译时?
A preprocessor directive, which would allow setting POLICY_LENGTH
already at compile time?
推荐答案
使用的sizeof()
。例如的sizeof(嗒嗒)
将评估为 5
在编译时(5,不是4,因为字符串字面总是包含一个隐含的空终止字符)。
Use sizeof()
. e.g. sizeof("blah")
will evaluate to 5
at compile-time (5, not 4, because the string literal always includes an implicit null-termination character).
这篇关于在编译期确定#define的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!