Core concepts of C + + array pointers, pointer arrays, function pointers

Source: Internet
Author: User

1. What is an array pointer?

Array pointer: A pointer to a one-dimensional or multidimensional array.

For example: int * b=new INT[10]; Pointer to a one-dimensional array B; Note that this time to release the space must be delete [], otherwise it will cause memory leaks, B becomes an empty cantilever pointer.
int (*B2) [10]=new int[10][10]; Note that the B2 here points to the first address of a two-dimensional array of int.
Note: Here, B2 is equivalent to a two-dimensional array name, but does not indicate its bounds, that is, the number of elements of the highest dimension, but its minimum number of dimensions must be specified! Just like a pointer to a character, which is equivalent to a string, do not refer to a pointer to a character as a pointer to a string. This is consistent with the nesting definition of the array.

int (*B3) [30] [20]; A three-level pointer ――> A pointer to a three-dimensional array;
int (*B2) [20]; second-level pointers;
b3=new int [1] [20] [30];
b2=new int [30] [20];
Two arrays are made up of 600 integers, which are three-dimensional arrays with only one element, each with a two-dimensional array of 30 rows and 20 columns, and the other a two-dimensional array with 30 elements, one-dimensional array of 20 elements for each element.
Delete these two dynamic arrays using the following formula:
delete [] B3; Delete (release) a three-dimensional array;
delete [] B2; Delete (release) a two-dimensional array;
Again: The type of b2 here is int (*), which represents a pointer to a two-dimensional array. B3 represents a pointer to (a pointer to a two-dimensional array), which is a level three pointer.


2. What is an array of pointers?

Array of pointers: An array is a pointer to the same type, which we usually call an array of pointers.

such as int * a[10]; it put 10 int * Type variable, because it is an array, already in the stack area allocated 10 (int *) space, that is, 32-bit machine is 40 byte, each space can hold an int variable address, At this point you can initialize each element of the array, or do a separate loop to initialize it.

Example: declaring an array of pointers is as follows: First, it is a 3-dimensional array, and the array contains a pointer that returns INT.

[CPP]View PlainCopy
    1. int *p[3];
    2. P[0] = new int[4];
    3. P[1] = new int[5];
    4. P[2] = new int[6];
    5. Delete P[0];
    6. Delete P[1];
    7. Delete P[2];

3. What is a function pointer?

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;
Int (*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 (*) ()); Get the size of a function pointer
Defines a typedef int (*PFUNC) () for a function pointer declaration type; 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 approach to define.

[Citation]http://blog.csdn.net/wuti_pl/article/details/6160856

http://blog.csdn.net/laoyang360/article/details/7269220

Core concepts of C + + array pointers, pointer arrays, function pointers

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.