在C指针参数处理 [英] Dealing with pointer argument in C
问题描述
我使用它具有以下签名的函数库:
I'm using a library which has a function with the following signature:
void LED_stop_blink_task ( void * callback_parameter );
void指针代表实际参数是uint32_t的指针,这是对领导板上的数字。
The actual parameter the void pointer stands for is a pointer to uint32_t, which is the number of the led on the board.
有没有办法来调用这个函数,而无需使用一个变量来保存数据?
在我的想象它会像
Is there a way to call this function without using a variable to hold the data ? In my imagination it will be like
LED_stop_blink_task(&35);
或唯一的办法是这样的:
or the only way is like this:
uint32_t led_num = 35;
LED_stop_blink_task(&led_num);
如果你问我为什么要扔变了,好了,我只是好奇,如果可能的话...
If you're asking why I want to throw the variable away, well, I'm just curious if it's possible...
推荐答案
在大多数平台有可能在无效简单的东西在
: INT
*
On most platforms it's possible to simply stuff the int
in a void *
:
LED_stop_blink_task((void *)32);
然后在功能,您可以转换为 INT
。
的整数可以被转换为任何指针类型。除
previously特定网络版,其结果是实现德科幻定义,可能不
正确对齐,可能不会指向引用的实体
类型,可能会重新presentation一个陷阱。
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
不限指针类型可被转换成一个整数类型。除
previously特定网络版,其结果是实现德网络定义。如果
结果不能重新在整型psented $ P $,该行为是
理解过程网络定义。结果不必是任何整数的值的范围
类型。
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
在实践中,这将在任何POSIX支持的平台上工作。例如TLPI说:
In practice this will work on any POSIX-supported platform. For example TLPI says:
严格地说,C类标准没有定义铸造的结果
诠释为void *,反之亦然。但是,大多数C编译器允许这些
操作,并且它们产生所希望的结果;也就是说,诠释J ==
(INT)((无效*)J)
Strictly speaking, the C standards don’t define the results of casting int to void * and vice versa. However, most C compilers permit these operations, and they produce the desired result; that is, int j == (int) ((void *) j).
这篇关于在C指针参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!