Analysis of function pointer array in C language

Source: Internet
Author: User

    • Discover problems
    • Problem analysis
    • Sample code
Discover problems

Today, when I read the source code for the socket in the Linux kernel, I encountered the following code:

structProto_ops {intFamilystructModule *owner;int(*release) (structSocket *sock);int(*bind) (structSocket *sock,structSockaddr *myaddr,intSockaddr_len);int(*connect) (structSocket *sock,structSockaddr *vaddr,intSockaddr_len,intFlags);int(*socketpair) (structSocket *SOCK1,structSocket *SOCK2);int(*accept) (structSocket *sock,structSocket *newsock,intFlags);int(*getname) (structSocket *sock,structSockaddr *addr,int*sockaddr_len,intPeer);unsigned int(*poll) (structFile *file,structSocket *sock,structPoll_table_struct *wait);int(*IOCTL) (structSocket *sock,unsigned intCmdunsigned LongARG);int(*listen) (structSocket *sock,intLen);int(*shutdown) (structSocket *sock,intFlags);int(*setsockopt) (structSocket *sock,intLevelintOptname,Char__user *optval,intOptlen);int(*getsockopt) (structSocket *sock,intLevelintOptname,Char__user *optval,int__user *optlen);int(*sendmsg) (structKIOCB *IOCB,structSocket *sock,structMsghdr *m, size_t Total_len);int(*recvmsg) (structKIOCB *IOCB,structSocket *sock,structMsghdr *m, size_t Total_len,intFlags);int(*mmap) (structFile *file,structSocket *sock,structVm_area_struct * VMA); ssize_t (*sendpage) (structSocket *sock,structPage *page,intOffset, size_t size,intflags);};

In this code, we notice that members of the Proto_ops struct include member variables such as the following:

int (*release)   (struct socket *sock);

This way, the function pointer is used as a struct member variable.

Problem analysis

First, we explain the differences between structs and C + + classes in C and C + +:

Structs in C are different from structs in C + +:
The struct in C can only customize the data type, and no function is allowed in the struct body;
Structs in C + + can join member functions.

The similarities and differences of structs and classes in C + +:
In the same place:
A struct can contain functions, you can also define public, private, protected data members, and after a struct is defined, you can create objects with struct names. However, structs in C do not allow functions, that is, in C + +, struct can have member variables, can have member functions, can inherit from other classes, can also be inherited by other classes, can have virtual functions.

The difference:
The members in the struct definition are public by default, and the members in the class definition are private by default. A non-static member function in a class has the this pointer, (there is no error in the struct, has been misled, has the this pointer as the member function of the test struct), and the class's keyword class can be used as the template keyword, and the struct cannot.

In fact, the struct in C only involves the data structure, not the algorithm, that is, the data structure and the algorithm are separated in C, and a class or a struct in C + + can contain functions (this function is commonly referred to as member functions in C + +), C + + The structure and class in this paper embody the combination of data structure and algorithm.
Therefore, when we read the pure C code, we should note that the function pointer member variables are used to implement the member function equivalent procedure in code.

Sample code

Here, we use a piece of code to explain the function pointer members:

#include <stdio.h>#include <stdlib.h>intFunc1 (intN) {printf("func1:%d\n", n);returnn;}intFunc2 (intN) {printf("Func2:%d\n", n);returnn;}intMain () {int(*a[2])(int); a[0] = func1; a[1] = Func2; a[0](1); a[1](2);return 0;}

We note that the code in the above

int (*a[2])(int);

In this code, we define an array like this:

Array to save pointers, what kind of pointers?
The Func function pointer, such as int func (int input), which is an int variable, returns an int variable.
Therefore, the array holds the parameter as a single int variable and the return value as int is worth the function pointer.

Now, we define an array like this, and then

    a[0] = func1;    a[1] = func2;

Since we declared and defined the FUNC1 and FUNC2 two functions before the main function (these two functions meet the previously mentioned function conditions), we can use these two function pointers to assign a function pointer array.
We can then use the array members to implement the function call:

    a[0](1);    a[1](2);

The end result is:

Analysis of function pointer array in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.