繁体   English   中英

无法理解 function 中 c 中的指针

[英]Unable to understand pointers in c in a function

我正在网站上阅读此代码。 我对编程相当陌生,所以请更详细地解释一下。

#include <stdio.h> 
// A normal function with an int parameter 
// and void return type 
void fun(int a) 
{ 
    printf("Value of a is %d\n", a); 
} 
  
int main() 
{ 
    // fun_ptr is a pointer to function fun()  
    void (*fun_ptr)(int) = &fun; 
  
    /* The above line is equivalent of following two 
       void (*fun_ptr)(int); 
       fun_ptr = &fun;  
    */
  
    // Invoking fun() using fun_ptr 
    (*fun_ptr)(10); 
  
    return 0; 
} 

怀疑——
我无法理解这种类型的声明和赋值void (*fun_ptr)(int) = &fun;
我的意思是,如果我们声明一个数据类型,那么我们就像int a; 并将其分配为a=10; 但在这里我们通过写(*fun_ptr)(10); . 请帮忙。

而不是这个记录

(*fun_ptr)(10);

你可以写

fun_ptr(10);

That is it is a function call of the function fun pointed to by the function pointer fun_ptr due to the initialization of that pointer in its declaration by the function address

void (*fun_ptr)(int) = &fun;

反过来,这个声明可以写得更简单,比如

void (*fun_ptr)(int) = fun;

因为在表达式中使用的 function 指示符(在本例中为fun ),例如初始化器被隐式转换为指向 function 的指针。

您可以通过以下方式为 function 类型使用 typedef 别名

typedef void Func( int );

在这种情况下,function 指针的上述声明可能看起来更简单,例如

Func *fun_ptr = fun;

这是使用 function fun的 function 类型的 typedef 重写的程序。

#include <stdio.h>

typedef void Func( int );

//  Function declaration without its definition using the typedef
//  This declaration is redundant and used only to demonstrate
//  how a function can be declared using a typedef name
Func fun;

//  Function definition. In this case you may not use the typedef name
void fun( int a )
{
    printf("Value of a is %d\n", a);
}

int main(void) 
{
    //  Declaration of a pointer to function
    Func *fun_ptr = fun;
    
    //  Call of a function using a pointer to it
    fun_ptr( 10 );

    return 0;
}

让我们通过使用类型别名和一些注释来稍微重写一下:

// Define a type-alias names fun_pointer_type
// This type-alias is defined as a pointer (with the asterisk *) to a function,
// the function takes one int argument and returns no value (void)
typedef void (*fun_pointer_type)(int);

// Use the type-alias to define a variable, and initialize the variable
// This defines the variable fun_ptr being the type fun_pointer_type
// I.e. fun_ptr is a pointer to a function
// Initialize it to make it point to the function fun
fun_pointer_type fun_ptr = &fun;

// Now *call* the function using the function pointer
// First dereference the pointer, to get the function it points to
// Then call the function, passing the single argument 10
(*fun_ptr)(10);

希望它能让事情变得更清楚。

以下是两句话的意思。

void (*fun_ptr)(int) = &fun; 这称为在同一行中声明和初始化fun_ptr ,这与执行int a = 10;相同。

(*fun_ptr)(10); 不是赋值语句,它是通过 function 指针fun_ptr调用 function fun

您还可以使用typedef创建一个使用 function 指针定义的新用户,并如上答案所示使用。

如果您是编程新手,这是一个高级主题, fun_ptr是指向 function 的指针。

声明:

void (*fun_ptr)(int) = fun;

意味着fun_ptr是指向 function 的指针,采用int参数并返回void ,如果使用指向 function fun的指针进行初始化。 (你不需要&

该行:

(*fun_ptr)(10);

没有分配任何东西,它调用了 fun_ptr 指向的fun_ptr ,但是很复杂

fun_ptr(10);

由于fun_ptr指向fun ,这等同于 `fun(10)。

Using a function pointer has its use eg in a sort function where the comparison function is passed in as a function pointer so the sorting can be different between calls. 达到同样的效果,而且在眼睛上更容易。

暂无
暂无

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

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