Home > Backend Development > C#.Net Tutorial > How to declare pointer to function in C?

How to declare pointer to function in C?

青灯夜游
Release: 2019-03-11 11:32:17
Original
4807 people have browsed it

In C, like ordinary data pointers (int *, char *, etc.), you can also declare a pointer to a function, that is, a function pointer. So how to declare it? The following article will show you how to declare function pointers. I hope it will be helpful to you. [Video tutorial recommendation: C Language Tutorial]

How to declare pointer to function in C?

How to declare a pointer to a function?

First let’s understand how to create a pointer to an integer in C?

int * ptrInteger; / *我们在int和ptrInteger之间放置了一个*运算符来创建指针。* /
Copy after login

The ptrInteger here is a pointer to an integer. If this can be understood, then logically we should not have any problem declaring pointers to functions.

Next let’s take a look at how to declare a function? For example:

int foo(int);
Copy after login

foo here is a function that returns and accepts an int type parameter. Therefore, logically one would think that simply placing a * operator between int and foo(int) should create a pointer to a function, i.e.:

int * foo(int);
Copy after login

However, this is not true; because, c operator precedence also comes into play here, operator () will take precedence over operator *. The above declaration means: a function foo, one of the parameters is of type int, and the return value is int*, which is an integer pointer; this is not what we want, so we also need to consider the c operator precedence.

So we have to bind the * operator with foo somehow. To do this, we will use the () operator to change the default precedence of C operators. Example: The

int (*foo)(int);
Copy after login

* operator is used with foo, and foo is the function name, thus declaring a pointer to the function.

Usage example of function pointer:

#include <stdio.h> 
// 具有int参数和void返回类型的普通函数
void fun(int a) 
{ 
    printf("a值为: %d\n", a); 
} 
  
int main() 
{ 
    // fun_ptr 是指向函数fun()的指针
    void (*fun_ptr)(int) = &fun; 
  
    /* 相当于下面两行
       void (*fun_ptr)(int); 
       fun_ptr = &fun;  
    */
  
    //使用fun_ptr调用fun()
    (*fun_ptr)(10); 
  
    return 0; 
}
Copy after login

Output:

How to declare pointer to function in C?

Note:

1. Unlike ordinary pointers, function pointers point to code, not data. Typically, a function pointer stores the start of executable code.

2. Unlike ordinary pointers, we do not use function pointers to allocate and deallocate memory.

3. In the function pointer, the name of the function can also be used to obtain the address of the function.

4. Like ordinary data pointers, function pointers can be passed as parameters or returned from functions.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to declare pointer to function in C?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template