如何将变量的地址转换并传递给 C 中的函数
假设我有一个 int
类型的变量,我想将其作为参数传递给 C 函数。该函数要求参数是long long
类型的指针。是否可以强制转换此变量,然后将其地址传递给函数,如下所示,而无需在程序中使用其他变量:
int val;
val = 10;
my_function((long long) &val);
void my_function(long long *parameter)
{
// do some operations with 'parameter'
}
不确定此代码是否具有隐藏效果。
Let's imagine that I have a variable of type int
which I would like to pass to a C function as a parameter. This function expects that the parameter is a pointer of type long long
. Is it possible to cast this variable and then pass its address to the function like this and without using additional variables in the program:
int val;
val = 10;
my_function((long long) &val);
void my_function(long long *parameter)
{
// do some operations with 'parameter'
}
Not sure if this code has hidden effects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可以。您必须传递正确的指针类型。
long long
是 8 个字节(可能),int
是 4 个字节(可能),如果你试图假装int
是一个long long
,当代码实际使用指针时,它将访问int
以及它旁边的另外 4 个字节,这会将事情搞砸。您实际上必须使用
long long
变量:No. You must pass the correct pointer type.
long long
is 8 bytes (probably),int
is 4 bytes (probably) and if you try to pretend anint
is along long
, when the code actually uses the pointer it will access theint
plus another 4 bytes next to it, which will screw things up.You must actually use a
long long
variable:强制转换运算符产生强制转换的标量对象的值。您不能获取该值的地址。例如,您需要引入一个中间变量,
但是如果您尝试这样做,那么程序的设计似乎有缺陷。例如,为什么变量
val
最初没有声明为 long long 类型?函数声明意味着它通过引用接受对象,并且传递的对象将在函数内更改,因为参数声明时没有限定符 const。因此,要么必须将变量 val 声明为 long long 类型,要么引入另一个通过引用接受 int 类型对象的函数。The cast operator yields the value of the casted scalar object. You may not take the address of the value. You need to introduce an intermediate variable as for example
But if you are trying to do that then it seems the design of your program has drawbacks. For example why is not the variable
val
initially declared as having the type long long? The function declaration means that it accepts objects by reference and the passed objects will be changed within the function because the parameter is declared without the qualifier const. So either you have to declare the variable val as having the type long long or to introduce another function that will accept objects of the type int by reference.正常情况
在正常情况下,
my_function
使用parameter
指向的long long
执行某些操作。那么一定有一个parameter
指向的long long
。您必须向函数提供一个long long
(或 C 2018 6.5 7 中别名规则允许的对象)或至少提供函数可以创建的内存空间。有一种方法可以在不创建额外变量的情况下做到这一点;您可以在函数调用中使用复合文字创建一个
long long
:但是,在函数返回后您将无法使用该
long long
的值。函数对long long
所做的任何更改都不会影响val
。异常情况
如果
my_function
不使用parameter
作为指向long long
的指针,无论其声明类型如何,那么您可以将其传递给其他类型已转换为long long
的指针。 C 标准允许此类用途,但这通常是不合适的代码,并且您可以传递哪些指针取决于my_function
正在做什么。Normal Case
In a normal case,
my_function
does something with along long
thatparameter
points to. Then there must be along long
thatparameter
points to. You must provide to the function either along long
(or an object that is permitted by the aliasing rules in C 2018 6.5 7) or at least the memory space where the function can create one.There is a way to do this without creating an additional variable; you can use a compound literal to create a
long long
in the function call:However, you will not be able to use the value of that
long long
after the function returns. Any changes the function makes to thelong long
will not affectval
.Abnormal Case
If
my_function
does not useparameter
as a pointer tolong long
, in spite of its declared type, then you can pass it some other pointer that has been converted to along long
. There are such uses permitted by the C standard, but this would generally be inappropriate code to write, and what pointers you can pass would depend on whatmy_function
is doing.