相关疑难解决方法(0)

是否有可能在可变参数宏中迭代参数?

我想知道是否有可能迭代传递给C99中的可变参数宏或使用任何GCC扩展的参数?

例如,是否可以编写一个通用的宏,它接受一个结构,并将其字段作为参数传递,并打印结构中每个字段的偏移量?

像这样的东西:

struct a {
    int a;
    int b;
    int c;
};

/* PRN_STRUCT_OFFSETS will print offset of each of the fields 
   within structure passed as the first argument.
*/

int main(int argc, char *argv[])
{
    PRN_STRUCT_OFFSETS(struct a, a, b, c);

    return 0;
}

c foreach c99 variadic c-preprocessor

66
推荐指数
5
解决办法
5万
查看次数

我们可以有递归宏吗?

我想知道我们是否可以在C/C++中使用递归宏?如果是,请提供示例.

第二件事:为什么我无法执行以下代码?我在做什么错?是因为递归宏吗?

# define pr(n) ((n==1)? 1 : pr(n-1))
void main ()
{
    int a=5;
    cout<<"result: "<< pr(5) <<endl;
    getch();
}
Run Code Online (Sandbox Code Playgroud)

c c++ macros c-preprocessor

50
推荐指数
4
解决办法
4万
查看次数

标签 统计

c ×2

c-preprocessor ×2

c++ ×1

c99 ×1

foreach ×1

macros ×1

variadic ×1