Analysis of function pointer array in C Language

Source: Internet
Author: User

Analysis of function pointer array in C Language
Problems Found Problem Analysis Sample Code

Problems Found

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

struct proto_ops {    int family;    struct module *owner;    int (*release)   (struct socket *sock);    int (*bind)      (struct socket *sock,                      struct sockaddr *myaddr,                      int sockaddr_len);    int (*connect)   (struct socket *sock,                      struct sockaddr *vaddr,                      int sockaddr_len, int flags);    int (*socketpair)(struct socket *sock1,                      struct socket *sock2);    int (*accept)    (struct socket *sock,                      struct socket *newsock, int flags);    int (*getname)   (struct socket *sock,                      struct sockaddr *addr,                      int *sockaddr_len, int peer);    unsigned int (*poll)     (struct file *file, struct socket *sock,                               struct poll_table_struct *wait);    int (*ioctl)     (struct socket *sock, unsigned int cmd, unsigned long arg);    int (*listen)    (struct socket *sock, int len);    int (*shutdown)  (struct socket *sock, int flags);    int (*setsockopt)(struct socket *sock, int level,                      int optname, char __user *optval, int optlen);    int (*getsockopt)(struct socket *sock, int level,                      int optname, char __user *optval, int __user *optlen);    int (*sendmsg)   (struct kiocb *iocb, struct socket *sock,                      struct msghdr *m, size_t total_len);    int (*recvmsg)   (struct kiocb *iocb, struct socket *sock,                      struct msghdr *m, size_t total_len,                      int flags);    int (*mmap)      (struct file *file, struct socket *sock,                      struct vm_area_struct * vma);    ssize_t (*sendpage)  (struct socket *sock, struct page *page,                      int offset, size_t size, int flags);};

In this Code, we noticed that the members of the proto_ops struct include the following member variables:

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

Here is how to use the function pointer As a struct member variable.

Problem Analysis

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

The differences between struct in C and struct in C ++:
The struct in C can only be a user-defined data type, and a function is not allowed in the struct;
The struct in C ++ can be added to member functions.

Similarities and differences between struct and classes in C ++:
Similarities:
Struct can contain functions, public, private, and protected data members, and struct names can be used to create objects. However, struct in C cannot have functions. That is to say, in C ++, struct can have member variables, member functions, and can be inherited from other classes, it can also be inherited by other classes and can have virtual functions.

Differences:
In the struct definition, the members are public by default, while in the class definition, the members are private by default. The non-static member function in the class has the this pointer. (it is not wrong in struct and has been misled. The member function tested by struct has the this pointer ), the class keyword can be used as the keyword of the template, but cannot be used by struct.

In fact, the struct in C only involves the data structure, not the algorithm. That is to say, in C, the data structure and algorithm are separated, in C ++, a class or a struct can contain functions (this function is usually called a member function in C ++ ), the structures and classes in C ++ reflect the combination of data structures and algorithms.
Therefore, when reading Pure C code, we should pay attention to the use of function pointer member variables in the code to implement the member function process in an equivalent way.

Sample Code

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

#include 
  
   #include 
   
    int func1(int n){    printf("func1: %d\n", n);    return n;}int func2(int n){    printf("func2: %d\n", n);    return n;}int main(){    int (*a[2])(int);    a[0] = func1;    a[1] = func2;    a[0](1);    a[1](2);    return 0;}
   
  

Pay attention to

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

In this Code, we define an array like this:

What kind of pointer does the array save?
It is like the func function pointer of int func (int input). If the input parameter is an int variable, the int variable is returned.
Therefore, the array stores the form parameter as a single int variable and the return value as the int value function pointer.

Now, we define such an array, and then

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

Since we declare and define func1 and func2 functions before the main function (these two functions meet the previously mentioned function conditions, we can use these two function pointers to assign values to the function pointer array.
Then, we can use array members to call functions:

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

The final result is:

Related Article

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.