Learning Summary of function pointer and pointer function _c language

Source: Internet
Author: User
Tags arrays getdate

A function pointer is a pointer to a function, which refers to the return value of a function as a pointer, but the following questions still feel confusing. Can you tell us more about this point?

(1) What is float (**def) [ten] def?
(2) double* (*GH) [a] GH is what?
(3) Double (*f[10]) () What is F?
(4) int* ((*B) [ten]) b What is it? This old feeling a bit messy, what is the trick to remember and understand a little bit clearly?

======================
Answer:

(1) def is a pointer to an object that is also a pointer, and the pointer eventually points to an array of 10 float.

(2) GH is the pointer, which points to an array of 10 elements, and the elements of the array are pointers to the double* type.

(3) F is an array of 10 elements, each of which is a pointer, the pointer to a function, the function type is parameterless and the return value is double. The following examples of tips are similar to this one.

(4) B is the pointer, which points to an array of 10 elements, and the array element is a pointer to the int* type.

The

tip is as follows:
If we come across a complex type declaration, how do we parse it? For example:
char (*a[3]) (int);
A What exactly is declared why Dongdong? Pointer? Array? Or a function? When

is parsed, it starts at the closest (by operator precedence) level. We see a the closest symbol is []--NOTE: * is lower than [] priority. A after a has [], then A is an array, and an array containing 3 elements.

What is the type of each element of this array? Although array a contains only a[0], a[1], a[2] three elements, a[3] is actually out of bounds, but when parsing the type of the elements of array A, we just need the formal element a[3]. Knowing the type of a[3], you know the type of the element of a. A[3] What is the type? Is the pointer, because its front has *. So, the element of array A is a pointer.

It is not enough to say that it is a pointer. For pointers, you must say what type of dongdong it is pointing to. What it points to is what, see *a[3] is what (a[3] is a pointer, it points to the Dongdong of course is *a[3]). Continuing to observe by priority, we see *a[3] followed by parentheses, so we can be sure that *a[3 is a function. That is, the element of array A is a pointer to a function. What type of function does the

point to? This is obviously a function of a type with an int and a return value of char.
The resolution is complete.
by the above method, the complex can also be resolved step-by-step.

Just as martial arts are not for beating people but for self-defense, we understand the above methods in order to understand the complex statements written by others, rather than to construct this complex in practice. When you really need a complex declaration, you can substitute a typedef for a part. For example, the above statement can be changed into two sentences:
typedef char (*FUN_PTR) (int);
Fun_ptr A[3];
That's a lot clearer.
In addition, the above analysis method makes us more clear about the nature of certain things. For example, the essence of an n-dimensional array is a one-dimensional array. See a specific example:
int a[3][5];
This statement is a one-dimensional array containing 3 elements, each of which is an array of 5 int. We cannot understand that a is a one-dimensional array containing 5 elements, each of which is an array of 3 int. Why? or according to the above method analysis, here Conlio.

Some books or online provides a "look to the right, to the left" approach, in fact, lack of versatility, such as it does not apply to the multidimensional array of the nature of the analysis. And this approach obscures the nature. The essence should be based on the above mentioned, according to the operator priority by layer stripping.

==============================================================================

One, the pointer function
When a function declares that its return value is 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 the return is an address, the type descriptor is generally int.
For example: int *getdate ();
int * AAA (Int,int);
The function returns an address value that is often used on an element address that returns an array.

Copy Code code as follows:

int * GetDate (int wk,int dy);
Main ()
{
int wk,dy;
Todo
{
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.

Second, function pointer
A pointer to a function contains the address of the function, which can be invoked to invoke the function. The declaration format is as follows:
Type descriptor (* Function name) (parameter)
In fact, this can not be called function name, should be called the variable name of the pointer. This particular pointer points to a function that returns an integer value. The declaration of a pointer must be consistent with the declaration it points to a function.
The parentheses outside the pointer name and the 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) ();
To assign a function's address to a function pointer, you can use the following two forms:
fptr=&function;
Fptr=function;
The address operator & is not required because a single function identifier represents its address on a label, and if it is a function call, it must also contain a parameter table enclosed in parentheses.
There are two ways to call a function by using pointers:
X= (*fptr) ();
X=fptr ();
The second format looks no different from a function call. But some programmers tend to use the first format because it explicitly indicates that a function is called by a pointer rather than a function name. Here's an example:

Copy Code code as follows:

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

Third, the pointer pointer
The pointer's pointer looks a bit confusing. Their declarations have two asterisks. For example:
char * * CP;
If there are three asterisks, that is the pointer to the pointer, four asterisks is the pointer to the pointer, and so on.
When you are familiar with simple examples, you can deal with complex situations. Of course, in the actual program, usually only two level pointers, three asterisks are not common, let alone four asterisks.
The pointer needs to use the pointer's address.
Char c= ' A ';
Char *p=&c;
Char **cp=&p;
A pointer to the pointer allows you to access not only the pointers it points to, but also the data pointed to by the pointer to which it points. Here are a few examples of this:
Char *P1=*CP; (&c)
Char C1=**CP;
You might want to know what's the use of such a structure? Pointers can be used to allow the called function to modify local pointer variables and handle array of pointers.

Copy Code code as follows:

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 then the address of the pointer is passed to the function Findcredit () as the argument. The Findcredit () function indirectly obtains the data in an array through an expression **FPP.

To traverse the array to find a negative value, the Findcredit () function is an object of 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 formal parameter pointer is directed. However, because the * operator is higher than the + + operator, the parentheses are required here, and if there are no parentheses, then the + + operator will be used on the double pointer FPP.

Four pointer to an array of pointers
Pointer to another use of the old processing pointer array. Some programmers prefer to use pointer arrays instead of multidimensional arrays, and a common use is to handle strings.

Copy Code code as follows:

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

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


The pointer nm is initialized with the address of the character pointer array names. Each call to printf () first passes the character pointer that pointer nm points to, and then the NM is added to point to the next element (or pointer) of the array. Note that the syntax described above is *nm++, which first takes the content that the pointer points to and then increases the pointer itself.

Note that the last element in the array is initialized to the 0,while loop to determine whether it is at the end of the array. A pointer with a value of 0 is often used as a terminator to a loop array. The programmer calls the 0-value pointer a null pointer (NULL). With a null pointer as a terminator, the code that iterates through the array is not necessarily altered when the tree or element is deleted, because the array still ends with a null 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.