The difference between a "go" pointer function and a function pointer

Source: Internet
Author: User

One

This "pointer function" and "function pointer" are easy to mistake in learning arm, so today, I want to make it clear at once and find some information, first of all, 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 (parameter table)

int *f (x, y);

First it is a function, except that 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, that is, 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 enclosed in parentheses (), and if it is included as a function pointer, the inverse is a pointer function.

Tell me more about it! Take a look 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 (parameter)
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 that is often used on the address of an element 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 well understood, and the child function returns the address of an element of the array. The output is the value in this address.

2, the function pointer is a pointer to a function variable, 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 contains the address of the function, which can be used to invoke the function. The declaration format is as follows:
        Type Descriptor (* Function name) (parameter)
In fact, this is not called the function name, it should be called the pointer variable name. 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. Without 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 take the following two forms:
fptr=&function;
Fptr=function;
the Fetch address operator & is not required because a function identifier alone represents its address, and if it is a function call, it must also contain a parameter table enclosed in parentheses.
    There are two ways to invoke a function with pointers:
x= (*fptr) ();
X=fptr ();
The second format looks like a function call. However, some programmers prefer to use the first format because it clearly indicates that a function is called by a pointer rather than a function name. Here's an example:

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 main difference is that one is a pointer variable and one is a function. In use is necessary to be clear to use correctly

Ii. pointers to pointers
    The pointer's pointer looks somewhat confusing. Their declarations have a two asterisk. For example:
Char * * CP;
If there are three asterisks, that is a pointer to the pointer pointer, four asterisks is a pointer to the pointer pointer, and so on. When you are familiar with simple examples, you can deal with complex situations. Of course, in the actual program, it usually only uses a level two pointer, three asterisks are uncommon, let alone four asterisks.
    The pointer's pointer needs to use the pointer's address.
Char c= ' A ';
Char *p=&c;
Char **cp=&p;
A pointer to a pointer can access not only the pointer it points to, but also the data it points to. Here are a few examples:
Char *P1=*CP;
Char C1=**CP;
You may wonder what the use of such a structure is. Pointers can be used to allow the called function to modify 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 as an argument 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 of the pointer to which the parameter pointer is pointing. However, because the * operator is higher than the + + operator, the parentheses are required here, and if there are no parentheses, the + + operator will be used on the double pointer FPP.

pointers to pointer arrays
    Pointer to a pointer to another usage array of old processing pointers. Some programmers prefer to use pointer arrays instead of multidimensional arrays, and a common use is to manipulate 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 pointer pointed to by the pointer nm, and then the NM is self-increment to point to the next element (or pointer) of the array. Note that the syntax described above is *nm++, which first obtains what the pointer points to and then makes the pointer increment.
    Note that the last element in the array is initialized to the 0,while loop to determine if it is at the end of the array. Pointers with a value of 0 are often used as terminators for loop arrays. The programmer calls the 0-value pointer a null pointer (NULL). Using a null pointer as the terminator, when the tree species additions and deletions, it is not necessary to change the code traversing the array, because the array still ends with a null pointer.

Ext.: http://www.cnblogs.com/gmh915/archive/2010/06/11/1756067.html

The difference between a "go" pointer function and a function pointer

Related Article

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.