Array and pointer, array pointer

Source: Internet
Author: User

Array and pointer, array pointer

1. dynamically allocate a one-dimensional array

Int * p = (int *) malloc (sizeof (int) * 10); // or int * p = new int [10];

2. dynamically allocate two-dimensional arrays 2X5

Int * p = (int *) malloc (sizeof (int) * 10); // or int * p = new int [10]; int (* pp) [5] = (int (*) [5]) p;

3. dynamically allocate a 3D array 12 = 3 (surface) x 2 (ROW) x 2 (column)

Int * p = (int *) malloc (sizeof (int) * 12); // or int * p = new int [12] int (* pp) [2] [2] = (int (*) [2] [2]) p;

Consider the memory allocation of the pointer array below.

A. One-dimensional pointer Array

1. array on stack for (int I = 0; I <10; I ++) {B [I] = new int;} for (int j = 0; j <10; j ++) {delete B [j];} // 2. int ** p = new int * [10]; // The array cannot be written as new (int *) [10]; for (int I = 0; I <10; I ++) {B [I] = new int;} for (int j = 0; j <10; j ++) {delete B [j];} delete [] p; // important

B. Two-Dimensional pointer Array

Int * B [4] [5]; // 10 pointer arrays with the content as pointers. // 1. array on Stack // 2. int ** p = new int * [20]; int * (* pp) [5] = (int * (*) [5]) p; // pointer to the two-dimensional pointer Array


The 32-bit pointer is always 4 bytes. the pointer type indicates the way variables are read.

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.