Do not hurt the pointer (4)--the relationship between the pointer and the function

Source: Internet
Author: User

A pointer can be declared as a pointer to a function.
int fun1 (char *,int);

Int (*pfun1) (char *,int);

PFUN1 = fun1;

int a = (*PFUN1) ("ABCDEFG", 7); Functions are called through function pointers. You can use a pointer as a parameter to a function. In a function call statement, you can use a pointer expression as an argument.

It is entirely conceivable that there is an object in the code area of the memory, through which the object is passed . A function can run this function. Our function names and function pointers are all pointing to this object. So the following kinds of calls are all legal.

#include <stdio.h>#include<stdlib.h>intFuncChar*R) {    intnum =0;  while(*s! =' /') {num+= *s; S++; }    returnnum;}intMainvoid){    CharStr[] ="ABC"; int(*pfunc) (Char*s); Pfunc=func; //a variety of legitimate callsprintf"%d\n", (*pfunc) (str));//call through object entityprintf"%d\n", (*func) (str));//call through object entityprintf"%d\n", func (str));//called by a pointerprintf"%d\n", Pfunc (str));//called by a pointerSystem ("Pause"); return 0;}

The function fun in this example counts the sum of the ASCII values of each character in a string. As I said earlier, the name of the array is also a pointer. in a function call, when the STR is passed to the parameter s as an argument, the value of STR is actually passed to the address that the s,s points to, but Str and s each occupy their respective storage space. The self-adding 1 operation of S in the function body does not imply that STR is also self-added to the 1 Operation .

Do not hurt the pointer (4)--the relationship between the pointer and the function

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.