简体   繁体   English

如何获取C宏中声明的变量的地址?

[英]How to get the address of a variable stated in C Macro?

I am new to C and am trying some macro statements. 我是C语言的新手,正在尝试一些宏语句。 I have a line like this: 我有这样一行:

#define write_data(src, TYPE, VALUE ) (write_implement(src, sizeof(TYPE), &(VALUE)))

And in a later function, I would like to use memcpy to copy the VALUE in another memory zone. 在以后的功能中,我想使用memcpyVALUE复制到另一个内存区域中。 Like this: 像这样:

void write_implement (void* src, int size_of_type, void* value)
{
    //whatever, just making the destination address from the source address
    void* dest = src + 4096;
    memcpy(dest, value, size_of_type);
}

The VALUE being passed in can be of any kind of data. 传入的VALUE可以是任何类型的数据。 That's why I am using void* to point to it and memcpy to copy the number of size of bytes. 这就是为什么我使用void *指向它并使用memcpy复制字节大小的原因。

But it doesn't work of course :) 但这当然不起作用:)

This is how I call the function: 这就是我所谓的函​​数:

write_data(addr, int, i*3); // i is a whatever integer variable

GCC gives me this: GCC给了我这个:

error: lvalue required as unary '&' operand 错误:需要左值作为一元'&'操作数

Does anyone have any idea how to find the address of the variable being passed in to the macro in order to allow me to make use of the address for copying? 有谁知道如何找到传递给宏的变量的地址,以便允许我使用该地址进行复制?

The later part of the macro can be changed (the "write_implement" and the parameters but not the "write_data" parameters). 可以更改宏的后半部分(“ write_implement”和参数,但不能更改“ write_data”参数)。 And the implementation part is also free to change. 并且实现部分也可以自由更改。

如果您的编译器支持C99复合文字,则可以执行以下操作:

#define write_data(src, TYPE, VALUE) write_implement(src, sizeof(TYPE), &(TYPE){ VALUE })

How about this: 这个怎么样:

#define write_data(src, TYPE, VALUE ) { \
        TYPE xxxx##__LINE__ = (VALUE); \
        write_implement(src, sizeof(TYPE), &(xxxx##__LINE__)); \
    }

It uses a somewhat "random" variable to store the value, before passing its address. 在传递其地址之前,它使用一个有点“随机”的变量来存储值。

When you expand the macro, you get for the third parameter, &(i * 3) , which makes no sense. 扩展宏时,获得第三个参数&(i * 3) ,这没有任何意义。 You can take the address of a variable, but not of an anonymous expression result. 您可以使用变量的地址,但不能使用匿名表达式结果的地址。

If you want to pass a value in using void* to hold the type, then you'd better have an actual variable named to hold it. 如果要使用void*传递值来保存类型,则最好有一个实际的变量来保存它。

int i = 5;
int j = i * 3;
write_data(addr, int, j);

I gotta say, though, to me calling the function directly is cleaner: 我得说,但是,对我来说直接调用函数更干净:

write_implementation(addr, sizeof(int), &j);

It's possible to do some C magic to make the macro call look the way you wanted, although I'd advise against it. 可以执行一些C魔术操作,以使宏调用看起来像您想要的样子,尽管我建议不要这样做。

#define write_data(src,type,value) \
    {type t = (value); write_implementation(src, sizeof(type), &t);}

write_data(addr, int, i*3);

And, as an aside, a C++ template would allow you to use the result of an expression the way you wanted as well, and a bit prettier (the key is the const ref). 顺便说一句,C ++模板将允许您以自己想要的方式使用表达式的结果,并且更漂亮(关键是const ref)。

template <typename T>
write_impl(T& dest, const T& src)
{
    memcpy(&dest, &src, sizeof(T));
}

// 'int' is the default type of 5*3
int intResult;
write_impl(intResult, 5*3);

// 'double' is the default type of 5.1*4.7
double doubleResult;
write_impl(doubleResult, 5.1*4.7);

// otherwise, have to cast
long longResult
write_impl(longResult, (long)5*3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM