是否可以在函数内部使用#define?

编程入门 行业动态 更新时间:2024-10-24 15:21:26
本文介绍了是否可以在函数内部使用#define?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如,我看到了如下的源代码.我们可以在函数中使用 #define 吗?它是如何工作的?(更多信息:此代码是我从 openvswitch 源代码复制的代码):

For example, I saw source code like the following. Can we use #define in a function? How does it work? (more information: this code is what I copied from openvswitch source code):

void * ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf) { switch (code) { case OFPUTIL_ACTION_INVALID: #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM: #include "ofp-util.def" OVS_NOT_REACHED(); #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf); #include "ofp-util.def" } OVS_NOT_REACHED(); } #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \ void \ ofputil_init_##ENUM(struct STRUCT *s) \ { \ memset(s, 0, sizeof *s); \ s->type = htons(ENUM); \ s->len = htons(sizeof *s); \ } \ \ struct STRUCT * \ ofputil_put_##ENUM(struct ofpbuf *buf) \ { \ struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ ofputil_init_##ENUM(s); \ return s; \ } #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ OFPAT10_ACTION(ENUM, STRUCT, NAME) #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ OFPAT10_ACTION(ENUM, STRUCT, NAME) #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \ void \ ofputil_init_##ENUM(struct STRUCT *s) \ { \ memset(s, 0, sizeof *s); \ s->type = htons(OFPAT10_VENDOR); \ s->len = htons(sizeof *s); \ s->vendor = htonl(NX_VENDOR_ID); \ s->subtype = htons(ENUM); \ } \ \ struct STRUCT * \ ofputil_put_##ENUM(struct ofpbuf *buf) \ { \ struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \ ofputil_init_##ENUM(s); \ return s; \ } #include "ofp-util.def"

推荐答案

#define 是预处理程序指令:用于生成最终的C ++代码在将其处理给编译器之前,将生成一个可执行文件.因此,代码如下:

#define is a preprocessor directive: it is used to generate the eventual C++ code before it is handled to the compiler that will generate an executable. Therefore code like:

for(int i = 0; i < 54; i++) { #define BUFFER_SIZE 1024 }

不执行了54次(在预处理器级别):预处理器只是在 for 循环上运行(不知道 for 的作用是什么)循环是),看到一个define语句,将 1024 与 BUFFER_SIZE 关联并继续.直到到达文件底部为止.

is not executed 54 times (at the preprocessor level): the preprocessor simply runs over the for loop (not knowing what a for loop is), sees a define statement, associates 1024 with BUFFER_SIZE and continues. Until it reaches the bottom of the file.

由于预处理器并不真正了解程序本身,因此您可以在任何地方编写 #define .

You can write #define everywhere since the preprocessor is not really aware of the program itself.

更多推荐

是否可以在函数内部使用#define?

本文发布于:2023-08-02 17:58:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   define

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!