The difference between a pointer function and a function pointer

Source: Internet
Author: User

One

I found this "pointer function" and "function pointer" easy to be mistaken in learning arm, so today I want to make it clear and find some information. First, the definition between them:

1, pointer function refers to a function with a pointer, that is, the essence is a function.

A function return type is a pointer to a type

Type identifier * function name (tables)

int *f (x, y);

First it is a function, just the return value of this function is an address value. The function return value must be accepted with a pointer variable of the same type, i.e.. The pointer function must have a function return value, and. In the keynote function. The function return value must be assigned to a pointer variable of the same type.

Said:

float *fun ();

float *p;

p = Fun (a);

Note that the pointer function is different from the function pointer representation method, so don't confuse it. The simplest way to discern is to see if the pointer in front of the function name is included in parentheses (), assuming that it is included as a function pointer. The inverse is the pointer function.

Let's be specific!

Please see below

pointer function:
When a function declares its return value as a pointer, it actually returns an address to the calling function for use in an expression that requires a pointer or address.
Format:
Type descriptor * Function name (number of parameters )
Of course, because an address is returned, the type specifier is generally int.


    For example:int *getdate ();
int * AAA (Int,int);
The function returns an address value. It is often used on an element address that returns an array.

int * GetDate (int wk,int dy);

Main ()
{
int wk,dy;
Do
{
printf (Enter Week (1-5) day (1-7) \ n);
scanf (%d%d,&wk,&dy);
}
while (wk<1| | wk>5| | dy<1| | DY&GT;7);
printf (%d\n,*getdate (wk,dy));
}

int * GetDate (int wk,int dy)
{
static int calendar[5][7]=
{
{1,2,3,4,5,6,7},
{8,9,10,11,12,13,14},
{15,16,17,18,19,20,21},
{22,23,24,25,26,27,28},
{29,30,31,-1}
};
Return &calendar[wk-1][dy-1];
}
The program should be very well understood, and the child function returns the address of an element of the array. The output is the value in this address.

2. A function pointer is a pointer variable that points to a function. That is, the essence is a pointer variable.

 Int (*f) (int x);/* declare a function pointer * /

f=func;/* assigns the first address of the Func function to the pointer f */

A pointer to a function includes the address of the function, which can be called by the function. The declaration format is as follows:
type specifier (* function name) (number of parameters )
In fact, this cannot be called a function name. A variable name that should be called a pointer.

This special pointer points to a function that returns an integer value. The declaration of the pointer is consistent with the declaration that it points to the function.
The parentheses outside the pointer name and pointer operator change the default operator precedence. Suppose there are no parentheses. It becomes a prototype declaration of a function that returns an integer pointer.


    For example:
void (*fptr) ();
assigning the address of a function to a function pointer can be used in the following two ways:
fptr=&function;
Fptr=function;
Fetch address operator & not necessary, because a single function identifier indicates its address as a label. The assumption is a function call. You must also include a parenthesis-enclosed reference.
    There are two ways to invoke a function with pointers, such as the following:
x= (*fptr) ();
X=fptr ();
the other format looks like a function call. But some program apes tend to use the first format, because it clearly indicates that a function is called by a pointer rather than a function name. Here are some examples:

void (*FUNCP) ();
void Filefunc (), Editfunc ();

Main ()
{
Funcp=filefunc;
(*FUNCP) ();
Funcp=editfunc;
(*FUNCP) ();
}

void Filefunc ()
{
printf (filefunc\n);
}

void Editfunc ()
{
printf (editfunc\n);
}

The program output is:
Filefunc
Editfunc

The basic difference is that one is a pointer variable and one is a function. In use is necessary to figure out the right use of talent

Two, pointer pointer
    
        char **  cp
     Suppose there are three asterisks, that is, pointers to pointers, four asterisks are pointers to pointers, and so on. When you are familiar with simple examples, you can cope with complex situations. Of course, in the actual program, it is generally only used to   two-level pointers, three asterisks are not common, let alone four asterisks.
&NBSP;&NBSP;&NBSP;&NBSP; The pointer's pointer needs to use the pointer's address.
        char c= ' A ';
        char *p=&c;
        char **cp=&p;
     through pointers to the pointer, you can access not only the pointers it points to, but also the data pointed to by the pointer it points to.

Here are a few examples:
Char *P1=*CP;
Char C1=**CP;
you might want to know what this structure is for. Pointers can be used to allow the called function to alter local pointer variables and manipulate array of pointers.



void Findcredit (int * *);

Main ()
{
int vals[]={7,6,5,-4,3,2,1,0};
int *fp=vals;
Findcredit (&AMP;FP);
printf (%D\N,*FP);
}

void Findcredit (int * * FPP)
{
while (**fpp!=0)
if (**fpp<0) break;
Else (*FPP) + +;
}

first, the pointer FP is initialized with the address of an array, and the address of the pointer is passed to the function Findcredit ().

The Findcredit () function indirectly obtains the data in the array through an expression **FPP. To iterate through the array to find a negative value, the object that the Findcredit () function is self-increment is the caller's pointer to the array, not its own pointer to the caller's pointer.

The statement (*FPP) + + is the self-increment operation of the pointer to which the shape-reference pointer is directed. However, because the * operator is higher than the + + operator, the parentheses are required here, assuming there are no parentheses. Then the + + operator will be used on the double pointer FPP.

pointers to pointer arrays
The pointer pointer also has an array of old processing pointers using the method.

Some program apes prefer to use pointer arrays instead of multidimensional arrays. A common use method is to handle strings.



Char *names[]=
{
Bill,
Sam,
Jim,
Paul,
Charles,
0
};

Main ()
{
Char **nm=names;
while (*nm!=0) printf (%s\n,*nm++);
}

first, the pointer nm is initialized with the address of the names array of character pointers. Each call to printf () first passes the character-type pointer pointed to by the pointer nm. The NM is then self-increment so that it points to the next element (or pointer) of the array. Note that the above-mentioned syntax is *nm++. It first obtains what the pointer points to, and then makes the pointer increment itself.
    Note that the last element in the array is initialized to the 0,while loop to infer if it is at the end of the array.

Pointers with 0 values are often used as terminators for loop arrays. The program Ape says the 0 value pointer is a null pointer (NULL). Using a null pointer as a terminator, you do not have to modify the code that iterates through the array when the tree is deleted. Because the array still ends with a null pointer.

The difference between a pointer function and a function pointer

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.