Parse pointer array, array pointer, function pointer, parse pointer Array Function

Source: Internet
Author: User

Parse pointer array, array pointer, function pointer, parse pointer Array Function

I. Memory Allocation for pointers and heap

Pointer ArrayA pointer is put in an array. We call it a pointer array.

Int * a [10]; because it is an array, it cannot apply for space from the heap space. You can only create a loop. Each element applies for a space, or each element points to another address space.
Array pointer: A pointer pointing to a unique or multiple unique arrays;

Int * B = new int [10]; pointer B pointing to a unique array;

Some examples:

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

Note that the b2 type here is int (*), which indicates a pointer to a two-dimensional array. Note that if you have applied for memory in the heap space, ensure that each space is released when the memory is released. B2. every element with a length of 10 is an int * pointer, and each int * Pointer Points to an array of int [10 ].


Int (** b3) [2] = new (int (*) [2]) [2]; b3 indicates a pointer to (pointing to a binary-only array, when applying for a space for him, we must pay attention to its type: it is a pointer of the int (*) type, and the type when applying for a space is int (*) [Number of elements], because it is followed by a number Suffix of two unique arrays per element, it is OK to add this suffix.
Int (** b4) [2]; indicates that each element points to an array (pointing to a binary-only array pointer.

(3) pointing to a (pointer to a pointer)
Int ** cc = new (int *) [10];
This statement is simple because the cc type is int * pointer, so you need to apply for it in the heap (int;
(4) A pointer array with multiple unique pointers.

Int ** d [2]; indicates an array of pointers pointing to pointers. There are two elements in the array, each element is a pointer, and this Pointer Points to another pointer :)

When we understand this, we first see 2, which indicates that there are two elements in this array. Then we can look at the type again, which indicates that each element in the array is of the int ** type and is a pointer to the pointer, when applying for memory, a pointer is generated for each element. If one pointer is directed at a time, an array of pointers must be directed to a pointer.

How can I change it to an array,
If you understand the above, the following statement is very simple:
D [0] = new (int *) [10];
D [1] = new (int *) [10];
---------------------------------------

Summary: as long as you know the pointer type, it is very easy to declare a pointer. The application in the heap is a little complicated.

(1) int * ptr; // The Pointer Points to an int type.

(2) char * ptr; // The Pointer Points to a char type.

(3) int ** ptr; // The type pointed to by the pointer is int *

(4) int (* ptr) [3]; // The type pointed to by the pointer is int () [3]

Ii. function pointer

For function pointers, I think we may need to write a function, which calls another function in the body. However, due to the limited progress of the project, we don't know what function to call, A function pointer may be required at this time;

Int a (); Declaration of this function;
Ing (* B) (); this is a declaration of a function pointer;
Let's analyze that the asterisks in the left Circular Arc are the key to function pointer declaration. The other two elements are the return type (void) of the function and the entry parameter in the incircle arc (in this example, the parameter is null ). Note that no pointer variable is created in this example-only the variable type is declared. Currently, you can use this variable type to create a type definition name and use the sizeof expression to obtain the size of the function pointer:
Unsigned psize = sizeof (int (*) (); returns the size of the function pointer.
// Define the function pointer declaration type
Typedef int (* PFUNC )();

PFUNC is a function pointer that points to a function without any input parameter and returns an int. Using this type definition name can hide the complex function pointer syntax. I strongly recommend that you use this method to define it;

Int (* f4 () [10] (); Declaration. f4 is a function that returns a pointer. This pointer points to an array containing 10 function pointers, these functions return an integer value. It is not a special feature of this function. It is also a good way for Bruce Eckel to say "recognize rules from the right to the left", which is worth learning.

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

It is very important to write at the end:
Mentioned in Objective C ++

If [] is used in the new expression, [] is also required in the corresponding delete expression. Otherwise, none are used.

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.