function pointers in C language learning notes _c language

Source: Internet
Author: User
Tags define function

One, define function pointers

Return_type (*func_pointer) (parameter_list)

Definition of common pointer variable

int * p;
char * pointer;

The qualification of the type is in front of the variable;
The definition of a function pointer type is both before and after, preceded by a return type, followed by an input parameter.

The above expression can be simplified by using typedef.

typedef return_type (*functionpointer) (parameter_list);
Functionpointer Func_pointer;

This is not easy to read, as in the above function, defines a return type of return_type, input parameter is parameter_list function pointer.

function to define the return function pointer
return_type (*function (func_parameter_list)) (parameter_list)

The return type of the box is a function pointer, and the remainder represents a function, and the input parameter is func_parameter_list.
It is equivalent to Functionpointer function (func_parameter_list);.
Look again:

void (*signal (int sig, void (* handler) (int))) (int);

Signal is a function that returns a function pointer, signal input as an int variable and a function pointer.

Third, the use of function pointers

#include <stdio.h> 
int Add (int a, int b); 
void Main () 
{ 
  int (*fun1) (int a, int b) = add; 
  Int (*fun2) (int a, int b) = &add; 
  Int (*FUN3) (int a, int b) = *add; 

  printf ("%d\n", Fun1 (1, 2)); 
  printf ("%d\n", fun2 (1, 2)); 
  printf ("%d\n", Fun3 (1, 2)); 

  Char input[10]; 
  Gets (input); 
} 
int add (int a, int b) 
{return 
  a + b; 
}

The function name is implicitly converted to a pointer, the front plus * and the & operator do not work, and printf results in 3.

Four, the Magic Code

Int (* (*PF ()) ()) ()
{return nullptr;}

Wow, what a function it is! Draw a frame to break it

A small box indicates that a function pointer is returned, in a large frame and a function pointer.
It means that PF () returns a function pointer, which corresponds to a function without input parameters: The return value is also a function pointer (a function that has no input parameter, and the return value is of type int). So complicated, a little dizzy!
Simplify with a typedef.

typedef int (*FUN1) (); 
typedef FUN1 (*FUN2) (); 
Fun2 PF () 
{return 
   nullptr; 
}

It's a lot more comfortable to see.

Five, this is what ghost!

(* (Void (*) ()) 0) ();

Draw a box to see:

The small box represents a function pointer, and a constant preceded by parentheses represents the cast of the type. Gee, it casts 0 into a function pointer, and executes! What is this operation Ah!
six or one-segment validation code

#include <stdio.h> 
typedef int Function (int, int); 
typedef int (*FUNCTIONPOINTER1) (int, int); 
typedef FunctionPointer1 (*FUNCTIONPOINTER2) (); 
int fun1 (int a, int b) 
{return 
  a + b; 
} 

FunctionPointer1 fun2 () 
{return 
  fun1; 
} 
FunctionPointer2 fun3 () 
{return 
  fun2; 
} 
Int (* (*FUN4 ()) ()) (int, int) 
{return 
  fun2; 
} 

void Main () 
{ 
  function* fuction = fun1; 
  FunctionPointer1 fun = fun1; 
  int a = FUN3 () () (3, 4); 
  int b = Fun4 () () (5, 6); 
  printf ("%d\n%d\n", A, b); 
  printf ("fun1:%d\n*fun1:%d\n&fun1:%d", Fun1, *fun1, &fun1); 
  printf ("fun:%d\n*fun:%d\n&fun:%d", Fun, *fun, &fun); 
  Char chars[10]; 
  Gets (chars); 
}

The function name is preceded by the *,& operator, which is an effect; the function pointer is preceded by a * operator is an effect, but the addition of the & operator represents the address of the fetch pointer.
Can be through the typedef int Function (int, int); An alias is defined for a function of a type, but only a variable in the form of a pointer is used:

function* fuction = fun1;

七、一个 problem
on the StackOverflow occasionally see the following questions, the code is as follows

#include
void Hello () {printf ("Hello");}
int Hello_1 ()
{
    printf ("Hello 1");
    return 0;
}
 
int main (void) {
  (*****hello) ();
  (****hello_1) ();
}

The result is that no matter how many pointer symbols are in the front of Hello, the Hello () function is executed, and "Hello" is printed.
Why is there such a result:
It is OK to point to a function with a pointer, but it is still converted to a functions pointer. Actually use the * to point to a function = = Call this function. So, no matter how many times you point to it, this function is still called.
Why is a function converted into a pointer? The answer is to convert the function default to function pointers, which can reduce the use of &, the compiler defaults to the function into function pointers, and save you every time you call the function with * call function.
Haha, as we said before, the function is the pointer. Seems a little less clear, and look at the following example

void foo () {
    printf ("foo to too!... \ n");
 
int a = 2;
int* Test ()
{return
    &a;
}
int main ()
{
  int i;
  void (*p1_foo) = foo;
  void (*p2_foo) = *foo;
  void (*p3_foo) = &foo;
  void (*p4_foo) = *&foo;
  void (*p5_foo) = &*foo;
  void (*p6_foo) = **foo;
  void (*p7_foo) = **********************foo;
 
  (*p1_foo) ();
  (*p2_foo) ();
  (*p3_foo) ();
  (*p4_foo) ();
  (*p5_foo) ();
  (*p6_foo) ();
  (*p7_foo) ();
  i = * (***test) ();
printf ("i=%d\n", I);
}

The above is not an exception to the above, can be normal printing of the data we want.
But for you, you have to make a careful analysis:
& for the operation of a function, is to return a pointer to a pointer to the function, if the pointer executes & is &&foo, it will return error, because &foo is a pointer value, that is, a rvalue type, and then for him & operation, is obviously returned to the error.

&&foo  //eroor
&*&*&*&*&*&*foo//ok
&******&foo  //OK

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.