Parse pointer array, array pointer, function pointer

Source: Internet
Author: User

One: Memory allocations for pointers and heaps

pointer array : A pointer is placed in an array, and we call him an array of pointers.

int * A[10]; Since it is an array, he cannot apply for space from the heap space.      can only be a loop, each element to apply for space, or each element to point to another address space.
array pointer : A pointer to a single or multi-only array;

int * B=new INT[10]; Pointer to a only array B;

Some examples:

Int (*b2) [10]=new int[10][10]; Notice that the B2 here points to the first address of a binary int array.

Note that the type of B2 here is int (*), which represents a pointer to a two-dimensional array. Again, if you have already applied for memory in the heap space, make sure that each space is freed when you release the memory. B2 is an array with a length of 10 for each element that is a int* pointer, each int* pointer to a int[10]


Int (**B3) [2]=new (Int (*) [2]) [2]; B3 represents a pointer to a pointer to a binary array, and we must pay attention to its type when we give him the space: it is a pointer to an int (*) type, which is an int when applying for space (* ) [Number of elements], since it also has a binary array per dollar number suffix, so add this suffix is OK.
Int (**B4) [2]; represents an array that each element points to (a pointer to a binary array).

(3) point to one (pointer to pointer)
int * * CC=NEW (int*) [ten];
This statement is simple because the CC type is a int* type pointer, so you need to apply (int *) in the heap to apply;
(4) array of pointers to the multi-pointer array.

int * * D[2]; represents an array of pointers to pointers, two elements in an array, each element is a pointer to another pointer:)

In the understanding of the first to see 2, so that the array has two elements, and then look at the type, which means that each element of the array is the int** type, is a pointer to the pointer, then the application of memory for each element corresponding to the pointer to generate pointers, if one-time point, you need to point to a pointer to an array of pointers

again how to change is also an array ah, hehe,
If you read the above, the statement below is simple:
d[0]=new (int *) [ten];
d[1]=new (int *) [ten];
--------------------------------------- 

Summary: As long as you know the pointer type, declaring a pointer is very simple, the heap application is slightly more complex, but know the type, it is a little cumbersome.

(1) The type that the int*ptr;//pointer points to is int

(2) The type that the char*ptr;//pointer points to is Char

(3) The type that the int**ptr;//pointer points to is int*

(4) int (*ptr) [3];//pointer to a type that is int () [3]

Two: function pointers

As for the function pointers, I think we might need to write a function in which the body of the function calls another function, but because of the limited progress of the project, we don't know what function to invoke, which may require a function pointer.

int A (); a declaration of this function;
ing (*b); This is a declaration of a function pointer;
let's analyze that the asterisk in the left parenthesis is the key to the function pointer declaration. The other two elements are the return type (void) of the function and the entry parameter (in this case, the argument is empty) from the edge parenthesis. Note that there is no pointer variable created in this example-only the variable type is declared. You can now use this variable type to create a type definition name and a sizeof expression to get the size of a function pointer:
unsigned psize = sizeof (int (*) ()); Gets the size of the function pointer
//For function pointer declaration type definition
typedef int (*PFUNC) ();

Pfunc is a function pointer that points to a function that does not have an input parameter and returns an int. Using this type definition name can hide complex function pointer syntax, as I strongly recommend that our inner disciples use this method to define;

Int (* (*F4 ()) [10] (), F4 is a function that returns a pointer to an array of 10 function pointers that return an integer value; it's not something special about this function, but the "right-to-left identification rule" that Bruce Eckel says is a good way , it's worth learning.

-------------------------------

Written on the last side, it's important:
Mentioned in effective C + +

If [] is used in the new expression, then [] is also required in the corresponding delete expression, and if it is not used, it is not used

Parse pointer array, array pointer, function 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.