The pointer to C + +

Source: Internet
Author: User

One: Related concepts

1. Array of pointers: int *p[6] is an array, which is an array of pointers, which is where the address is stored.

2. Array pointer : Int (*p) [6] is a pointer to an array.

3. The function pointer : int* (*fun) (int *p1,int *p2) is a pointer to a function where the return value of the function is an address.

4. Array of function pointers: int * (*FUN[6]) (int *p1,int *p2) is an array that holds an array of function pointers.

5. pointer to an array of function pointers: int * (* (*fun) [6]) (int *p1,int *p2) is a pointer , pointer to an array, the element of the array is a function pointer.

Do you see a little bit of regularity??? You can read an array of pointers in this way, add A pointer to an array, a pointer to a function, see what the attribute is, and what this variable represents.

Two: Explanation

1. Array of pointers

First, this is an array, and the array is the pointer, emphasizing arrays, arrays, arrays. There are also how many bytes of an array are determined by the array itself. It is the abbreviation for "array of stored pointers".

Here we need to understand the priority between a symbol of the problem, “[ ]” “ * ” higher than the priority, P1 will first and “[ ]” combine to form an array, the array named P, int * decorated with the contents of the array,

That is, the elements of the array. Then int *p[4]; it is clear that it is an array that has 4 int pointers to the data type, that is, an array of pointers.

int *p[6];
Cout<<sizeof (p[0]) <<ENDL;//8
Cout<<sizeof (P) <<endl;//48

A pointer occupies four nodes on a 32-bit machine and occupies 8 bytes on a 64-bit machine.

2: Array pointers

Now it's clear, it's a pointer, it's pointing to the array, the pointer, the pointer, the pointer, the pointer is pointing to an array, it's int (*p)[4]; better explained,

Here () the precedence is higher than [] the precedence, then P is the first and the * Union, the p is a pointer, p is the pointer variable name. intmodifies the contents of an array, that is, each element of an array.

The array here has no name, is an anonymous array, and P is a pointer to an int array that contains 4 types of data, an array pointer.

int (*p) [6];
Cout<<sizeof ((*P) [1]) The byte size of the second element in the array of <<ENDL;//4 output, because each data type is of type int, so it accounts for 4 bytes
Cout<<sizeof (p) <<endl;//8 the byte size at which p is output, p is a pointer, so P is eight bytes.

equal to Int (*) [6] p;

3. Function pointers

With it int * (*) (int *p1,int *p2)fun;

This is a function pointer, fun is a pointer variable name, point to a function, the function has two parameters, the integer pointer p1 and the character of this pointer P2, the function's return type is an integer pointer type.

4. Array of function pointers

int* (*fun[6])(int* p1, int* p2) ;It is an array, and the array name is fun. There are 6 pointers to functions stored in the array. I'll change it, and you're looking.int* (*)(int* p1, int* p2) fun[6] ;

These 6 pointers to functions point to. The point is to understand an array at this point.

5. Array of function pointers

int* (*(*)[6])(int* p1,int* p2) funLet me explain: fun First is a pointer to an array containing 6 elements, and the array contains pointers to functions;

These pointers refer to functions that return a value type to a pointer to an integral type, and a pointer to an integer.

Finally, attach a code to see others on the Internet;

(* (Void (*) ()) 0) ();
Call the function at address 0, which has no arguments and returns a function of type void
The code is to turn the 0 strong into a function pointer, and then dereference to find the function and then call

void (*signal (int, void (*) (int)))) (int);
is a function declaration, signal is a function,
Arguments are of type int and function pointer type, return value is function pointer type

The pointer to C + +

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.