语法求解~

Volftooth 2011-12-25 10:35:44
GOPT 的源码、

#define gopt_start(...) (const void*)( const struct { int k; int f; const char *s; const char*const*l; }[]){ __VA_ARGS__, {0}}
#define gopt_option(k,f,s,l) { k, f, s, l }


求详解~~看不懂~
...全文
181 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shangwu_35 2011-12-30
  • 打赏
  • 举报
回复
先说中间的部分吧:

定义了 结构体数组的无名变量,维度是2,用__VA_ARGS__可变参列表初始化其中一个结构元素,{0}初始第二个元素。

然后用void* 指针指向这个结构体数组首地址


你调用的时候 void *pvoid = gopt_start(3,3,"string","string1");

实际就是这样 pvoid 指向了这个东西{{3,3,"string","string1"},{0}}

AnYidan 2011-12-26
  • 打赏
  • 举报
回复
可变参数的宏
SDKSC 2011-12-26
  • 打赏
  • 举报
回复
貌似在C语言陷阱和缺陷这本书中有些比较像的函数定义········小白路过
ccjsj1 2011-12-26
  • 打赏
  • 举报
回复
define 宏定义替换
赵4老师 2011-12-26
  • 打赏
  • 举报
回复
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。
Volftooth 2011-12-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhao4zhong1 的回复:]

引用 8 楼 volftooth 的回复:
VS2010 编译不过~

1>f:\volftooth's works\pc\sxs\sxs\sxs.c(185): warning C4116: unnamed type definition in parentheses
1>f:\volftooth's works\pc\sxs\sxs\sxs.c(185): error C2059: ……
[/Quote]

改了错误更多了、

我就想知道 (const void*)( const struct { int k; int f; const char *s; const char*const*l; }[]){ __VA_ARGS__, {0}} 是什么意思?、
结构体还没有名字?、后面的大括号又是什么意思?、
赵4老师 2011-12-26
  • 打赏
  • 举报
回复
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_vclang/html/51e757dc-0134-4bb2-bb74-64ea5ad75134.htm
C++ Language Reference
Variadic Macros

Variadic macros are function-like macros that contain a variable number of arguments.

Remarks
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.

The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.

Example
Copy Code
// variadic_macros.cpp
#include <stdio.h>
#define EMPTY

#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)

int main() {
CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n"); // won't print

CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)\n"); // won't print
CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)\n");

// always invokes printf in the macro
CHECK3("here %s %s %s", "are", "some", "varargs3(5)\n");

MACRO("hello, world\n");
// MACRO("error\n", EMPTY); would cause C2059
}


Output

here are some varargs1(1)
here are some varargs2(4)
here are some varargs3(5)
hello, world


See Also
Reference
Macros (C/C++)

赵4老师 2011-12-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 volftooth 的回复:]
VS2010 编译不过~

1>f:\volftooth's works\pc\sxs\sxs\sxs.c(185): warning C4116: unnamed type definition in parentheses
1>f:\volftooth's works\pc\sxs\sxs\sxs.c(185): error C2059: syntax error : '{'
[/Quote]
将后缀改为.cpp再试试?
Volftooth 2011-12-26
  • 打赏
  • 举报
回复
VS2010 编译不过~

1>f:\volftooth's works\pc\sxs\sxs\sxs.c(185): warning C4116: unnamed type definition in parentheses
1>f:\volftooth's works\pc\sxs\sxs\sxs.c(185): error C2059: syntax error : '{'
赵4老师 2011-12-26
  • 打赏
  • 举报
回复
VS的话,编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。
黯然 2011-12-25
  • 打赏
  • 举报
回复
我勒个去...宏定义替换..
Volftooth 2011-12-25
  • 打赏
  • 举报
回复
我发详细点吧~

#define gopt_start(...) (const void*)( const struct { int k; int f; const char *s; const char*const*l; }[]){ __VA_ARGS__, {0}}
#define gopt_option(k,f,s,l) { k, f, s, l }
#define gopt_shorts( ... ) (const char*)(const char[]){ __VA_ARGS__, 0 }
#define gopt_longs( ... ) (const char**)(const char*[]){ __VA_ARGS__, NULL }
void *options= gopt_sort( & argc, argv, gopt_start(
gopt_option('h', 0, gopt_shorts('h', '?'), gopt_longs("help", "HELP")),
gopt_option('v', 0, gopt_shorts('v'), gopt_longs("version")),
gopt_option('s', GOPT_ARG, gopt_shorts('s'), gopt_longs("secu_flag")),
gopt_option('c', GOPT_ARG, gopt_shorts('c'), gopt_longs("cid")),
gopt_option('S', 0, gopt_shorts('S'), gopt_longs("sim_unlock")),
gopt_option('f', 0, gopt_shorts('f'), gopt_longs("free_all")),
gopt_option('w', GOPT_ARG, gopt_shorts('w'), gopt_longs("disable_wp")),
gopt_option('k', GOPT_ARG, gopt_shorts('k'), gopt_longs("disable_kf")),
gopt_option('b', GOPT_ARG, gopt_shorts('b'), gopt_longs("hboot")),
gopt_option('r', GOPT_ARG, gopt_shorts('r'), gopt_longs("restore")),
gopt_option('y', GOPT_ARG, gopt_shorts('y'), gopt_longs("recovery")),
gopt_option('d', 0, gopt_shorts('d'), gopt_longs("debug"))));
猪头小哥 2011-12-25
  • 打赏
  • 举报
回复
你把他们在哪里用到的程序贴出来看看

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧