Annoying function pointer (1), annoying function pointer

Source: Internet
Author: User

Annoying function pointer (1), annoying function pointer

Link: http://www.cnblogs.com/AnnieKim/archive/2011/11/20/2255813.html

This article is written to deepen your memory. I found that it is hard to remember a lot of knowledge without repeated efforts and hands-on practices.

1) initialize the function pointer.

The function is as follows:

Int CompareString (const string & str1, const string & str2)
{
Return str1.compare (str2 );

}

There are two methods to initialize a function:

The first and most common method:

Int (* CompareFunction) (const string &, const string &) = CompareString;

The second method is to use typedef to define the function type, which helps you understand the code:

Typedef int (* CompareFunctionType) (const string &, const string &);
CompareFunctionType CompareFunction = CompareString;

2) function pointer assignment.

The function name can be understood as a pointer to a function of this type. Of course, the address operator can also generate a pointer to this type of function on the function name. That is to say, the following two values are feasible:

CompareFunctionType CompareFunction = CompareString;
CompareFunctionType CompareFunction = & CompareString;

3) function call.

Whether it is called by function name, function pointer, or explicit pointer symbol, the method is the same:

CompareString ("abc", "CBA ");
CompareFunction ("abc", "CBA ");
(* CompareFunction) ("abc", "CBA ");

4) array of function pointers.

For the array of function pointers, we strongly recommend that you use typedef to define the type before using it. Otherwise, it will affect the readability of the Code. The preceding example is used as an example:

// Without typedef
Int (* CompareFunctionArray [3]) (const string &, const string &);
// With typedef
CompareFunctionType CompareFunctionTypeArray [3];

5) function pointers are used as types of function return values.

At this step, we will find out how easy typedef is to use. Otherwise, I cannot fully understand the meaning of the following statement:

// Without typedef
Int (* func (int *, int) (const string &, const string &);

The preceding Declaration declares func (int *, int) as a function. The returned value is a function pointer and the function type is int (*) (const string &, const string &).

How obscure!

If you write typedef, you don't have to worry about it. The role of typedef is as follows:

CompareFunctionType func (int *, int );

6) pointer to the extern "C" function.

C ++ primer 3 states that pointers to C functions are different from pointers to C ++ functions, but many compilers now have language extensions, the pointers of these two functions have the same characteristics.

So I tried it in vs 2010 and the results proved that it supports this language extension.

The function declaration is as follows:

Extern "C" int InsideFunctionC (const string & str1, const string & str2)
{
Return str1.compare (str2 );
}

Int InsideFunctionCPlusPlus (const string & str1, const string & str2)
{
Return str1.compare (str2 );

}

Function pointer initialization and calling. values can be assigned to the pointer pointing to the C function:

Int (* CompareFunction) (const string &, const string &) = InsideFunctionC;

In addition, when extern "C" is applied to a declaration, all functions declared by it will be affected. For example:

Extern "C" void OutSideFunction (int (* fc) (const string &, const string &))
{
Cout <fc ("abc", "CBA") <endl ;;
}

Both OutSideFunction and fc will be affected by extern "C", but the vs2010 compiler supports a pointer to the C ++ function as the parameter of OutSideFunction. As follows:

Int main ()
{
OutSideFunction (InsideFunctionC );
OutSideFunction (InsideFunctionCPlusPlus );

Return 0;

}

This is almost the case. I read it once yesterday and wrote a blog today to review it again. It should be a better memory. Giggle.



A c function pointer

The combination of this statement is generally from the middle, and then to the right-left-right, of course there is a bracket priority, the following for detailed analysis:
First look at the middle brackets (float (*) (long), char *)),
The analysis starts from the middle, and the (*) In the middle indicates that it is a pointer. Then we can see that there is a long parameter on the right (*) (long, therefore, we can determine that this is a pointer to the function, and then look at the float (*) (long) on the left, indicating that the function pointed to by this pointer returns a float type return value. Look at the second char *. char * Indicates a string.
In this case, we encounter parentheses (* f (float (*) (long), char *). We can see f, which is closest to (float (*) (long), char, here we can see that f (float (*) (long), char *), f is a function with (float (*) (long), char *) as a parameter table, then look at the number *, indicating that the returned value is pointer type. Then, combined with the (double) on the Right, it indicates that the pointer returned by f is a function pointing to the double parameter, and then look at the final int, it indicates that the pointer returned by f is an int type that points to the return value of a function with double as the parameter.

Write an example of returning a function pointer using a pointer Function

# Include <stdio. h>

Int * position (int (* func) (void )){
Return (int *) func;
}

Int another (void)
{
Return 0;
}

Int main ()
{
Printf ("% X \ n", * position (another ));
Return 0;
}

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.