Deep understanding of [pointer function], [function pointer], [pointer pointer], [pointer]__ function to pointer array]

Source: Internet
Author: User

pointer function

1, the pointer function refers to the function with a pointer, that is, the essence is a 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.

A function return type is a pointer to a type:

Format:

type identifier * Function name (parameter table)

int *match (void *key_x,void *key_y);

Parsing: First of all, it's a function, except that the return value of this function is an address value . The function return value must be accepted with the same type of pointer variable, that is, the pointer function must have a function return value, and, in the calling function, the function return value must be assigned to the same type of pointer variable .

For example:

float *match ();

float *p;

p = Match (a);


Important Knowledge Points:
Note that the pointer function differs from the function pointer representation method, and do not confuse it. The simplest way to discern is to see if the pointer * number in front of the function name is contained by parentheses (), if it is included, the function pointer, or the pointer function.


pointer function

2, the function pointer is to point to the function of the pointer variable, that is, the essence is a pointer variable.

The function pointer three points to an executable snippet or a pointer to an information block that calls an executable snippet , rather than a pointer to some data. function pointers Store and manage functions as normal data.


The format is as follows:

Type Descriptor (* Function name) (parameter)

Int (*match) (int x);/* Declare a function pointer /*

int *f = match;/* assigns the first address of the match function to the pointer f */


PS:

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. However, the first format is recommended because it explicitly indicates that the function is invoked through a pointer rather than a function name.

The main difference between a function pointer and a pointer function is a pointer variable and a function.


Pointer to Pointer  

Third, the pointer pointer

For example:
char * * data;

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. In the actual program, the general also use only two level pointers, three-level pointers and four-level pointers are rarely used.


The pointer needs to use the pointer's address.


char c = ' A ';
Char *p = &c;
Char **data= &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;
Char C1=**CP;
Pointers can be used to allow the called function to modify local pointer variables and handle array of pointers, reference to the online example:


void Findcredit (int * *);
Main ()
{
    int vals[]={7,6,5,-4,3,2,1,0};
    int *fp=vals;
    Findcredit (&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.

pointer to array of pointers

Four pointer to an array of pointers
Another use of a pointer pointer is to handle an array of pointers. One of the most common uses is to handle strings.

Char *names[]=
{
     Zhangsan,
     Lisi,
      Wangwu,
      Maliu,
      Fu,
      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.

The last element in the array is initialized to the 0,while loop once 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. Where the 0 value pointer is a null pointer (NULL).

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.