Case: Do not understand CC ++ function pointer

xiaoxiao2021-03-05  31

Patient: Doctor, I toss for a long time and I can't understand the function pointer. Chinese medicine: I can realize that I don't understand, not bad. Then you talk about your current understanding. What is the function pointer? Patient: The function pointer is a pointer to the function. Chinese medicine: What is the function? Patient: The function is a function. TCM: Not a pointer to the function? Patient: Naturally not. Chinese medicine: How can the function give a value to function pointer? Is Int can assign a value to int *? Patient: This. . . . . . Chinese medicine: Is the logic not pass? Patient: Yes, what is going on? Chinese medicine: This problem is put on hold, I ask you, what is a pointer? Patient: It is the variable of the address. Chinese medicine: What is put in the function pointer? Patient: Function entrance address. Chinese medicine: So function pointer is the variable of the function entry address? Patient: (Carefully) I agree. TCM: The function is the constant of the function of the entry address. Patient: Wow! This is a good explanation! Function assignment to function pointer just like assigning constants to the same type variable! Chinese medicine: Is there a problem? Patient: Yes, "The function is the constant of the function entry address." This sentence is not passed. The function is something to put your own entrance address? Chinese medicine: 孺 word can be taught. Here "Function Inlet Address" is a word and cannot be removed. The real function, nothing more than a piece of code, there is no "one piece of code" in C / C , and only the entrance address of "a code" entry address is described. Function and function pointer. Patient: I understand, "The function pointer is the pointer" and "function (2) of the function (1)" and "function (2) does not point to the pointer of the function (3)," "function (1)" is what you just said. " True function ", function (2) (3) refers to" function "in the meaning of C / C syntax! Chinese medicine: Well, make sense. So is there any problem? Patient: "The function pointer is a pointer to the function." This sentence firms a misleading person, but C / C grammar has also played the role of promoting the flock. // # include type typedef int (* fn_haha) (); int real_haha () {Return Printf ("haha / n");} void main (int Argc, char * argv []) {fn_haha Haha = Real_haha; real_haha () (* haha) ();} // Since Haha and Real_haha are something at a hierarchy, why is it called "Real_haha ();", one (* haha) ()? It is obvious that it is engaged in differentiation and brain. Doctor: Even C / C syntax, you dare to criticize, strong! As you said, this is not a good grammar, so now compiler, such as VC and GCC, is allowed to use Haha (); instead of traditional (* haha) (); you will know. As for the book, I wrote (* haha) (); I can only say that I have been in myself for a long time, and I finally threw my book. Patient: My disease is good, I will turn it back.

转载请注明原文地址:https://www.9cbs.com/read-34021.html

New Post(0)