如何将变量的地址转换并传递给 C 中的函数

发布于 2025-01-16 23:06:47 字数 327 浏览 2 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏天碎花小短裙 2025-01-23 23:06:47

不可以。您必须传递正确的指针类型。

long long 是 8 个字节(可能),int 是 4 个字节(可能),如果你试图假装 int 是一个 long long,当代码实际使用指针时,它将访问 int 以及它旁边的另外 4 个字节,这会将事情搞砸。

您实际上必须使用 long long 变量:

long long llval = val;
my_function(&llval);
val = llval; // if needed

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 an int is a long long, when the code actually uses the pointer it will access the int plus another 4 bytes next to it, which will screw things up.

You must actually use a long long variable:

long long llval = val;
my_function(&llval);
val = llval; // if needed
焚却相思 2025-01-23 23:06:47

强制转换运算符产生强制转换的标量对象的值。您不能获取该值的地址。例如,您需要引入一个中间变量,

int val;

val = 10;

long long tmp = val;
my_function( &tmp );

但是如果您尝试这样做,那么程序的设计似乎有缺陷。例如,为什么变量 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

int val;

val = 10;

long long tmp = val;
my_function( &tmp );

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.

故事与诗 2025-01-23 23:06:47

正常情况

在正常情况下,my_function 使用 parameter 指向的 long long 执行某些操作。那么一定有一个parameter指向的long long。您必须向函数提供一个 long long (或 C 2018 6.5 7 中别名规则允许的对象)或至少提供函数可以创建的内存空间。

有一种方法可以在不创建额外变量的情况下做到这一点;您可以在函数调用中使用复合文字创建一个 long long

my_function(& (long long) {val});

但是,在函数返回后您将无法使用该 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 a long long that parameter points to. Then there must be a long long that parameter points to. You must provide to the function either a long 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:

my_function(& (long long) {val});

However, you will not be able to use the value of that long long after the function returns. Any changes the function makes to the long long will not affect val.

Abnormal Case

If my_function does not use parameter as a pointer to long long, in spite of its declared type, then you can pass it some other pointer that has been converted to a long 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 what my_function is doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文