C ++ array, two-dimensional array, function parameter, array two-dimensional array

Source: Internet
Author: User

C ++ array, two-dimensional array, function parameter, array two-dimensional array

One-dimensional array:

1 # include <iostream> 2 using namespace std; 3 int main () 4 {5/** all types of arrays are int type */6 int numbers [5] = {1, 2, 3, 4, 5 }; 7/** access the array by pointer */8 int * p = numbers; 9 cout <"access by normal pointer: \ n "; 10 for (int I = 0; I <5; I ++) 11 cout <p [I] <"; 12 cout <endl; 13/** 14 numbers is interpreted as the address of the first element of the array, that is, & numbers [0], and it is also a constant 15. For example: sizeof numbers is the array size 16 */17 cout <"array size:"; 18 cout <sizeof numbers <"Byte. \ n "; // 5*419/** the address for accessing the array 20 array through the array pointer is & numbers NOTE: this is the same as the value of numbers !!! 21. You can point to the same address using a normal pointer or an array pointer pointing to 22. The difference is that their pointer operation is 23 */24 int (* p2) [5] = & numbers; // Replace the numbers of the array with (* p2), which means that the array pointer has 25 cout <"access via pointer array: \ n "; 26 for (int I = 0; I <5; I ++) 27 cout <(* p2) [I] <""; 28 cout <endl; 29/** 30 int * p and int (* p2) [5] The difference 31 is that their pointer operation 32 does not understand Baidu pointer operation 33 */34 cout <"p =" <p <endl; 35 cout <"p + 1 =" <p + 1 <endl; // 4Byte36 cout <"p2 =" <p2 <endl; 37 cout <"p2 + 1 =" <p2 + 1 <endl; // 20 Byte38/** when it comes to the array pointer, it is required to mention another knowledge point pointer array 39 array pointer is Pointer 40 pointer array is defined by array 41, the two are usually confused 42 */43 int * p3 [5]; // each element of the pointer array is a pointer of 44 for (int I = 0; I <5; I ++) 45 p3 [I] = (* p2) + I; // p2 is an array pointer that is often used as a pointer to a two-dimensional array using 46 cout <"I do not know what this operation is: \ n"; 47 for (int I = 0; I <5; I ++) 48 cout <* p3 [I] <"; 49 cout <endl; 50 return 0; 51}

 

Two-dimensional array:

1 # include <iostream> 2 using namespace std; 3 int main () 4 {5 int data [3] [5] = 6 {7 {1, 2, 3, 4, 5 }, 8 {9, 8, 5}, 9 {, 9, 5, 1} 10 }; 11/** the essence of a two-dimensional array is the array, 12 the First-dimensional array is the address of the three one-dimensional arrays, and the second-dimensional array is the ordinary array */13 int * p [3] = {data [0], data [1], data [2]}; 14 cout <"Address: \ n"; 15 for (int I = 0; I <3; I ++) 16 cout <p [I] <""; 17 cout <endl; 18/** regular access to 2D arrays */19 cout <"regular access to 2D arrays \ n"; 20 for (int I = 0; I <3; I ++) 21 {22 (Int j = 0; j <5; j ++) 23 cout <data [I] [j] <"; 24 cout <endl; 25} cout <endl; 26/** use a pointer array to try accessing */27 cout <"Use a pointer array to skin \ n "; 28 for (int I = 0; I <3; I ++) 29 {30 for (int j = 0; j <5; j ++) 31 cout <p [I] [j] <""; // The reason for the access is that the size is int32 cout <endl; 33} cout <endl; 34/** the first element of 35 int data [3] [5] With an array pointer is data [0]. Then its address is & data [0], which is equivalent to data36. data is the address of the first element, and it's still a constant 37. So what is the pointer to & data [0 ]? What it looks like? 38 data [0] is of the int [5] type, so 39 & data [0] is of the int (*) [5] 40 */41 int (* p2) type) [5] = data; 42 cout <"access \ n with array Pointer"; 43 for (int I = 0; I <3; I ++) 44 {45 for (int j = 0; j <5; j ++) 46 cout <p2 [I] [j] <""; // refer to the array pointer of the one-dimensional array to understand 47 cout <endl; 48} cout <endl; 49 return 0; 50}

Arrays and functions:

1 # include <iostream> 2 using namespace std; 3 void print1 (const int * p, int len); 4 void print2 (const int p [], int len ); 5 void print3 (int p [] [5], int len); // don't use const DON't !!! 6 void print4 (int (* p) [5], int len); // don't use const DON't !!! 7 int main () 8 {9 int numbers [5] = {1, 2, 3, 4, 5}; 10 int data [3] [5] = 11 {12 {1, 2, 3, 4, 5}, 13 {5, 6, 7, 8, 9}, 14 {9, 8, 7, 6, 5} 15}; 16 print1 (numbers, 5); 17 print2 (numbers, 5 ); 18 print3 (data, 3); 19 print4 (data, 3); 20 return 0; 21} 22 void print1 (const int * p, int len) 23 {24 cout <"print1 \ n"; 25 for (int I = 0; I <len; I ++) 26 cout <p [I] <"; 27 cout <endl; 28} 29 void print2 (const int p [], int len) 30 {31 cout <"print2 \ n"; 32 for (int I = 0; I <len; I ++) 33 cout <* (p + I) <''; 34 cout <endl; 35} 36 void print3 (int p [] [5], int len) 37 {38 cout <" print3 \ n "; 39 for (int I = 0; I <len; I ++) 40 {41 for (int j = 0; j <5; j ++) 42 cout <p [I] [j] <''; 43 cout <endl; 44} cout <endl; 45} 46 void print4 (int (* p) [5], int len) 47 {48 cout <"print4 \ n"; 49 for (int I = 0; I <len; I ++) 50 {51 for (int j = 0; j <5; j ++) 52 cout <p [I] [j] <''; 53 cout <endl; 54} cout <endl; 55} 56/** 57 [] ([0]) the same 58 as * indicates that [0] 59 p [5] can be written as * (p + 5) 60 p [5] what we actually do is first addressing and then in the Process of value 61 tan haoqiang talked about 62 in his book */

Supplement:

1 # include <iostream> 2 using namespace std; 3 int main () 4 {5/** 6 const int * p and int * const p Differences 7 */8 int a = 10; 9 const int B = 20; 10 const int * p1; // * p1 is read-only11 p1 = & a; 12 cout <"p1 =" <p1 <endl; 13 cout <"& a =" <& a <endl; 14 cout <"* p1 =" <* p1 <endl; 15 // ERROR * p1 = 23; 16 p1 = & B; // p1 is not read-only17 cout <"p1 = & B \ n "; 18 cout <"* p1 =" <* p1 <endl; 19 20 int * const p2 = &; // p2 is read-only and must init it21 // int * const p2 = & B; mismatch because data is not protected 22 // const int * const p2 = & B; in this way, 23 cout <"p2 =" <p2 <endl; 24 cout <"* p2 =" <* p2 <endl; 25 int c = 1222; 26 // p2 = & c; erro p2 is read-only27 * p2 = 1234567; 28 cout <"* p2 =" <* p2 <endl; 29 cout <"p2 =" <p2 <endl; 30 return 0; 31}

 

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.