C宏中的类型

C宏中的类型,c,macros,types,C,Macros,Types,我正在尝试使用C语言,我认为编写以下宏是明智的: // takes address of a variable and passes it on as a void* #define vp_pack(v) ((void*)&x) // takes a void pointer and dereferences it to t #define vp_upack(v,t) (*((t*)v)) 我这样测试: int main() { int five = 5; char

我正在尝试使用C语言,我认为编写以下宏是明智的:

// takes address of a variable and passes it on as a void*
#define vp_pack(v) ((void*)&x)

// takes a void pointer and dereferences it to t
#define vp_upack(v,t) (*((t*)v))
我这样测试:

int
main()
{
    int five = 5;
    char a = 'a';

    vp ptr;

    // Test 1
    ptr = vp_pack(five);
    if(vp_unpack(ptr,int) == five)
        printf("Test 1 passed\n");
    else
        fprintf(stderr, "Test 1: all is doomed\n");

    // Test 2
    ptr = vp_pack(a);
    if(vp_unpack(ptr,char) == a)
        printf("Test 2 passed\n");
    else
        fprintf(stderr, "Test 2: all is doomed!\n");
}
但是,
gcc
向我吐出淫秽的话,比如
error:int前面的预期表达式

我花了一整天的时间在这上面,但仍然不明白代码到底出了什么问题。有人能给我解释一下吗

而且,我的职业不是程序员。我甚至不在里面工作,但这是我的爱好。所以如果有人能告诉我一个更好的方法,请告诉我


谢谢

你所做的应该有用,你只是有两个小错误:

#define vp_pack(v) ((void*)&v)
#define vp_unpack(v,t) (*((t*)v))

第一行是
x
,而不是
v
,第二行是
vp\u upack
定义vp\u pack(v)((void*)&x)
看起来不对,是打字错误吗?x=是的。下面的答案是正确的。