C + + Fundamentals 8 "difficult" review: array pointers, function pointers, function pointers do function parameters C language polymorphism

Source: Internet
Author: User

1, array pointer syntax grooming

Review how the array data types are defined:

Review, how to define an array of pointer types:

Review, how to define a pointer to an array type directly:

2, function pointer syntax grooming

1) How to define a function type

2) How to define a function pointer type

3) How to define a function pointer (point to the entry address of a function)




"The only way for intermediate programmers to go to senior programmers"

1, function type parameter of function

Pass the entry address of the function, strange effect: "Polymorphic is so"

Analysis of function pointer function parameter thought



1, array pointer syntax grooming

Review how the array data types are defined:

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int main ( {typedef int (arr) [5];//defines a data type for arr myarr;//with data types, defining variables myarr[3] = 10;//using cout << myarr[3] << endl;int a[5];/ /equivalent to write return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 10[email protected]://9904870 26.blog.51cto.com~/c++$


Review, how to define an array of pointer types:

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int main ( {typedef int (*ARR) [10];//defines a data type arr my_arr;//with data type declaration a variable int a[10];//regular array my_arr = &a;//zone address to me (*my_arr) [0] = 9999 ; cout << * ((*my_arr) +0) << Endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 9999[email protected]://99048 7026.blog.51cto.com~/c++$



Review, how to define a pointer to an array type directly:

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int main ( {int Arr[10];int (*P) [ten];p = &arr; (*p) [2] = 10;cout << * (* (p) + 2) << Endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 10[email protected]://9904870 26.blog.51cto.com~/c++$


2, function pointer syntax grooming

1) How to define a function type

2) How to define a function pointer type

3) How to define a function pointer (point to the entry address of a function)


1, the basic invocation of the function:

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int Add ( int A,int b) {return a + B;} int main () {int sum = Add (or//function name) is the entry address of the function cout << sum <<endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 3[email protected]://99048702 6.blog.51cto.com~/c++$


1) How to define a function type

Use function pointers to call functions indirectly

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int Add ( int A,int b) {return a + B;} int main () {typedef int (fun) (int a,int b);//define a fun function type fun *p = null;//define a variable p = &add;//return value, parameter type exactly the same function address, give me p = ad                       D Write this, compatible with c history int sum = P (1,6); cout << sum <<endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 7[email protected]://99048702 6.blog.51cto.com~/c++$


2) How to define a function pointer type

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int Add ( int A,int b) {return a + B;} int main () {typedef int (*fun) (int a,int b);//define a fun function type fun p = null;//defines a variable p = add;//so write, compatible with c history int sum = P (1,6); cout &L t;< sum <<endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 7[email protected]://99048702 6.blog.51cto.com~/c++$





3) How to define a function pointer (point to the entry address of a function)

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;int Add ( int A,int b) {return a + B;} int main () {int (*fun) (int a,int b);//define a variable fun = add;int sum = Fun (1,6); cout << sum <<endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 7[email protected]://99048702 6.blog.51cto.com~/c++$


"The only way for intermediate programmers to go to senior programmers"


1, function type parameter of function

Get ready:

[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;typedef Int (*fun) (int a,int b);//define a data type int add (int a,int b) {return a + B;} int main () {fun F = null;//uses the data type, defines a variable f = &add;//assigns variable variables cout << f (0) << Endl;return;} [Email protected]://990487026.blog.51cto.com~/c++$ g++-g-o run main.cpp &&./run 3[email protected]://99048702 6.blog.51cto.com~/c++$


[Email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp #include <iostream>using namespace Std;typedef Int (*fun) (int a,int b);//define a data type int add (int a,int b) {return a + B;} The following two ways are the same as int chunli_1 (fun f) {return F (3,5);} int chunli_2 (int (*fun) (int a,int b)) {return fun (2,3);} int main () {cout << chunli_1 (add) << endl;cout << chunli_2 (add) << Endl;return 0;} [Email protected]://990487026.blog.51cto.com~/c++$!gg++-g-o run main.cpp &&./run 85[email protected]://99048 7026.blog.51cto.com~/c++$




Pass the entry address of the function, strange effect: "Polymorphic is so"

[email protected]://990487026.blog.51cto.com~/c++$ cat main.cpp  #include  <iostream >using namespace std;typedef int  (*fun) (int a,int b);//define a data type INT&NBSP;ADD1 ( INT&NBSP;A,INT&NBSP;B) {cout <<  "add1  function is called  \n"; return a + b;} INT&NBSP;ADD2 (int a,int b) {cout <<  "add2  function is called  \n"; return a +  b;} INT&NBSP;ADD3 (int a,int b) {cout <<  "add3  function is called \ n"; return a + b;} INT&NBSP;ADD4 (int a,int b) {cout <<  "add4  function is called \ n"; return a + b;} The following two ways are the same as Int chunli_1 (fun f) {return f (3,5);} Int chunli_2 ( int  (*fun) (int a,int b)) {return fun (2,3);} Int main () {cout <<  chunli_1 (ADD1) << endl;cout <<   Chunli_2 (ADD2) << endl;cout <<  chunli_2 (ADD3) <&Lt; endl;cout <<  chunli_2 (ADD4) << endl;return 0;} [email protected]://990487026.blog.51cto.com~/c++$ g++ -g -o run main.cpp  The  && ./run add1  function is called  8add2  function is called  5add3  function is called 5add4  function is called 5[ email protected]://990487026.blog.51cto.com~/c++$



Analysis of function pointer function parameter thought

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/83/DB/wKiom1d-F7ajpEOXAAGHMXGPX_k665.jpg "title=" 01_ The function pointer does the function parameter thought analysis. jpg "alt=" wkiom1d-f7ajpeoxaaghmxgpx_k665.jpg "/>














This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1812376

C + + Fundamentals 8 "difficult" review: array pointers, function pointers, function pointers do function parameters C language polymorphism

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.