C language advanced programming-function pointers, program design function pointers

Source: Internet
Author: User

C language advanced programming-function pointers, program design function pointers
The function pointer points to the code area, and the normal Pointer Points to the data area. The function pointer is defined as follows:Return type (* function pointer variable) (parameter table)For example, void (* pf) () refers to a function like void f. Example of using function pointers:

Void open (); // declare void print (); void exit (); void (* pfs []) () = {open, print, exit} for several functions }; // function pointer array int main () {int I; void (* pf) (); pf = open; (* pf )(); ///// call open () for (I = 0; I <3; I ++) // call open () and print () in sequence (), exit () function (* pfs [I]) ();} void open () {printf ("open \ n");} void print () {printf ("print \ n");} void exit () {printf ("exit \ n ");}
From this example, we can see that the function name itself is the address of the function code area. There are several confusing definitions. Consider the following:
void (*getInterrupt(int no))();void setInterrupt(int no,void (*pf));LRESULT (*lpfnWndProc)(HWND,UNIT,WPARM,LPARAM);
The first declaration indicates the prototype declaration of getInterrupt. It has an int type parameter, and the return value is a pointer similar to void f () function. Actually, after getInterrupt (int no) is run, it is the return value of this function. the second declaration indicates the prototype declaration of the setInterrupt function. The return value is void, which has two parameters, the int type and the pointer to the void f () function. The third Declaration defines a pointer variable lpfnWndProc, which refers to a function in the shape of LRESULT wndproc (HWND, UNIT, WPARM, LPARAM.

C language programming, functions and pointers, help write a few questions

1. Compile the function fun. The function is to calculate and output the average value of n courses and count the number of courses above the average value. This number is returned as the function value.

Int fun (float a [], int n)
{
Double sum = 0.0;
Double ave = 0.0;
Int count = 0;
For (int I = 0; I <n; I ++)
Sum + = a [I];
Ave = sum/n;
For (int I = 0; I <n; I ++)
If (a [I]> = ave)
Count ++;
Return count;
}

2. Compile the function fun. The function is: Find All integers between 1 and m (including m) that can be divisible by 7 or 11 and put them in array a. Return these integers through n.

Void fun (int m, int * a, int * n)
{
* N = 0;
For (int I = 1; I <= m; I ++)
{
If (I % 7 = 0 & I % 11 = 0)
{
A [* n] = I;
(* N) ++;
}
}
}

3. Compile the function fun, count the number of times that the 26 lowercase letters a to z appear in the string referred to by tt, and store them in the array referred to by pp in sequence.

Void fun (char * tt, int pp [])
{
For (int I = 0; I <26; I ++)
Pp [I] = 0;
For (int I = 0; I <strlen (tt); I ++)
{
Pp [tt [I]-'a'] ++;
}
}

4. Compile the function fun. The function is to convert all the letters in the string indicated by the ss into uppercase letters (if the position is not a letter, it is not converted ).

Void fun (char * ss)
{
For (int I = 1; I <strlen (ss); I + = 2)
{
If (ss [I] <= 'Z' & ss [I]> = 'A ')
{
Ss [I] + = 'a'-'A ';
}
}
}
5. Compile the function fun to delete all spaces in the string.

Void fun (char * str)
{
Int count = 0;
For (int I = 0; I <strlen (str); I ++)
{
If (str [I]! = '')
{
Str [count] = str [I];
Count ++;
}
Else
{
Str [count] = 0;
}
}
}

C language pointer pointing to function programming exercises

# Include "stdio. h "//
Void fun0 (int n ){
Printf ("this is the print output of the % d function. \ N ", n + 1 );
}
Void fun1 (int n ){
Printf ("this is the print output of the % d function. \ N ", n + 1 );
}
Void fun2 (int n ){
Printf ("this is the print output of the % d function. \ N ", n + 1 );
}
Void main (void ){
Void (* ptr [3]) (int) = {fun0, fun1, fun2 };
Int n;
Printf ("Type 1, 2, or 3 \ nn = ");
Scanf ("% d", & n );
N --;
Ptr [n] (n );
Printf ("\ n ");
}

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.