Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What is the difference between C language pointer function pointer? Better have an example? Be sure to adopt
What is the difference between C language pointer function pointer? Better have an example? Be sure to adopt

I don't think the concept of pointer function is of great significance. . . . Perhaps its greatest significance is to confuse beginners' audio-visual with function pointers.

pointer function refers to the function that returns a pointer, such as our common gets (), strcpy (), strcat (), malloc () and so on. It really doesn't make sense to treat it as a new concept alone. These functions don't have any * * * properties except returning a pointer, but the returned pointer is far from perfect.

let's talk about function pointers.

pointers are divided into three types: object pointers, function pointers and void pointers.

the declaration form of function pointer is T (*p) (...), in which *p is enclosed in parentheses because the priority of function type resolution is higher than that of pointer resolution, so parentheses are used to prevent parsing into the above pointer function (in fact, there is a pair of parentheses between the two).

a function pointer can be bound to a function with the same return value and parameter type. For example: int? foo(int,? double); ? //? Function declaration

int? (*bar)(int,? double)? =? foo; ? //? The function pointer

is written as & Foo is also possible.

the biggest feature of a function pointer is that it can't do unary+-operation (of course, it can't do+=-=+-etc.), and the result of unary * operation on it is itself. For example: (following the above example) bar(1,1.);

(*bar)(1,1.); ? //? Two situations are equivalent

even you can do this: (* * * * * * * * * * * * * bar) (1,1.);

still right.

Therefore, it is more natural to adopt a method similar to function call in practical application.

Just to test it, there is a more subtle problem:

Functions in C can use prototypes or not. (Not using the prototype is considered obsolete)

Prototype is to declare the parameter types and numbers in the function header, and not using it is a simple pair of brackets. For example: int? foo(); ? //? Non-prototype

int? bar(void); ? //? Prototype

int? foo1(int); ? //? Prototype

What's the impact? Take a look at the following code: int? foo(int? i){return? i; }? //? Prototype

int? foo1(double? c){return? (int)c; }? //? Prototype

int? bar(){return? 1; }? //? Non-prototype

int? main(void)

{

int? (*foobar)(int); ? //? Prototype

int? (*barfoo)(); ? //? Non-prototype

foobar? =? foo;

foobar? =? & foo;

foobar? =? bar;

foobar? =? foo1; ? //? this

barfoo? =? foo;

barfoo? =? bar;

barfoo? =? foo1;

foobar(1);

(*foobar)(1);

(*************foobar)(1);

return? ;

}

My compiler warned the line this refers to, reminding me that the pointer type does not match.

We can see that non-prototype function pointers can be bound to any function, while function pointers with prototypes can play a stricter type restriction when binding.

it's easy to overlook that functions (with prototypes) that don't accept any parameters are declared in int foo(void); Void should not be omitted, especially when using function pointers of corresponding types. If void is omitted, the wrong binding will not be warned.

I also noticed a phenomenon: int? foo(char);

int? (*bar)()? =? foo;

this code also gives a warning. Why?

because char, short, float, etc. will be implicitly promoted to their corresponding higher-level types without using prototypes.

in this binding, the bar is non-prototype, and then integer promotion may be performed, while foo has a prototype, so promotion is not required when calling. This may lead to errors.

the above is the point that should be paid attention to when using function pointers. However, remind LZ that it is better to use function pointers with prototypes in the process of using function pointers.