The understanding of pointers

Source: Internet
Author: User
Tags printf

1. Array of pointers

It is an array, the elements of the array are pointers, and how many bytes of the array are determined by the array itself,

The abbreviation is: An array of stored pointers.

"Example" int *p1[10];

Analysis: "[]" priority is higher than "*", P1 first with "[]" to form an array of names, the array named P1;int * is decorated with the contents of the array, that is, each element of the array. It's going to be

Know: This is an array, this array has 10 elements, each element is an int type of pointer,

As shown in Figure 1.

2. Array pointers

It is a pointer to an array, which is always 4 bytes under a 32-bit system, and the number of bytes it points to is not known, for short: A pointer to an array.

"Example" Int (*P2) [10];

Analysis: The priority of "()" is higher than "*", P2 first with "*" to form a pointer definition, the pointer variable named P2,int decorated is the contents of the array, that is, each element of the array. And the array here does not have a name,

is an anonymous array. This is known as: This is a pointer to the pointer named P2, which points to an array with 10 elements, as shown in Figure 2.

3. Function pointers

3.1 Meaning of the function pointer:

A function pointer is a pointer to a function. It is a pointer to a function.

3.2 Role of function pointers

The advantage of using a function pointer is that you can unify multiple modules that implement the same function to identify the system structure

Clearer and easier to maintain later. Even for layering, facilitating system abstraction, reducing coupling, and making interfaces

Realized separately.

3.3 Understanding Function pointers

1) Char *fun (char *p1, char *p2);

Analysis: Fun is the function name, P1 and P2 are two parameters of the function, the parameter type is char * type, the return value is char * type.

2) char * *fun (char *p1, char *p2);

Analysis: Fun is the function name, P1 and P2 are two parameters of the function, the parameter type is char * type, the return value is char * * type.

3) char * (*fun) (Char *p1, char *p2)

Analysis: This is a pointer, p is a pointer variable, point to a function, this function has two char * type parameter, return

The return value is char * type. Just a function pointer.

3.4 Recognize the function pointer--* (int *) &p

Cases

# include <stdio.h>

# include <stdlib.h>

void Function ()

{

printf ("Call function!\n");

}

int main ()

{

void (*p) ();

* (int *) &p = (int) Function;

(*p) ();

System ("pause");

return 0;

}

Operation Result:

Analysis:

1) void (*p) (), this line of code defines a pointer variable p,p points to a function, and the function's arguments and return values are void types.

2) &p is the address of the pointer variable p itself, which is a 32-bit shaped binary constant (32-bit system). AN (int *) &p represents a pointer to cast &p into an shaping.

3) function means that the address of the function entry is cast to reshape. The following: "* (int *) &p = (int) Function;" Indicates that the address of the function entry is assigned to the pointer variable p.

4) "(*p) ();" means a call to a function.

Note: There is no difference between a function pointer and a normal pointer, except that it points to something different.

3.5 Understanding Function Pointers--(* (Void (*) ()) 0) ()

The first step: void (*) (), which is a function pointer, no function name, and the parameter and return value of the function are void type.

Step Two: (Void (*) ()) 0, which means that shaping 0 is cast to a function pointer, and 0 is an address, that is, a function is saved in a region with the first address of 0.

Step Three: (* (Void (*) ()) 0), which is the contents of a section of memory that takes the beginning of the 0 address, is the function that is stored in a section of the first address 0.

Fourth Step: (* (Void (*) ()) 0) (), which represents the function call.

3.6 Use of function pointers

Cases

# include <stdio.h>

# include <stdlib.h>

# include <string.h>

Char *fun (char *p1,char *p2)

{

int i = 0;

i = strcmp (P1,P2);

if (0 = = i)

{

return P1;

}

Else

{

return P2;

}

}

int main ()

{

int i = 0;

char * (*PF) (char *p1,char *p2);

Pf = &fun;//assigning values to function pointers

Char *ret = (*PF) ("AA", "BB");

for (i = 0; i < 8; i++)

{

printf ("%c", Ret[i]);

}

printf ("\ n");

System ("pause");

return 0;

}

Operation Result:

Note: When assigning a value to a function, it can be fun with &fun or directly with a functional name. Because, the function name is compiled as an address.

4. Array of function pointers

An array of function pointers, which is an array that holds pointers to functions.

"Example" char * (*pf[3]) (char *p)

Analysis: It is an array with an array named PF and 3 pointers to functions are stored in the array. These pointers point to the parameters

is a pointer to a char type, and the return value is a pointer to a char type.

# include<stdio.h>

# include <stdlib.h>

Char *fun1 (char *p)

{

printf ("%s\n", p);

return p;

}

Char *fun2 (char *p)

{

printf ("%s\n", p);

return p;

}

Char *fun3 (char *p)

{

printf ("%s\n", p);

return p;

}

int main ()

{

char * (*pf[3]) (char *p);

Pf[0] = fun1;//can be used directly with the function name

PF[1] = &fun2;//can also be used with the function name plus the fetch address character

PF[2] = &fun3;

Pf[0] ("fun1");

Pf[1] ("fun2");

Pf[2] ("fun3");

System ("pause");

return 0;

}

The result of the operation is:

5. Pointer to function pointer array

Pointer to an array of function pointers, which is a pointer to an array that holds pointers to functions.

"Example" char * (* (* (* PF) [3]) (char *p)

Analysis: PF is a pointer variable that points to an array of 3 elements that hold a pointer to a function. These pointers point to a pointer that returns a char type, and the argument is a pointer to a char type.

# include <stdio.h>

# include <stdlib.h>

Char *fun1 (char *p)

{

printf ("%s\n", p);

return p;

}

Char *fun2 (char *p)

{

printf ("%s\n", p);

return p;

}

Char *fun3 (char *p)

{

printf ("%s\n", p);

return p;

}

int main ()

{

char * (*a[3]) (char *p);

char * (* (*PF) [3]) (char *p);

PF = &a;

A[0] = fun1;//can be used directly with the function name

A[1] = &fun2;//can also be used with the function name plus the fetch address character

A[2] = &fun3;

Pf[0][0] ("fun1");

Pf[0][1] ("fun2");

Pf[0][2] ("fun3");

System ("pause");

return 0;

}

The result of the operation is:

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.