Learning point C language (19): Data Type-array

Source: Internet
Author: User
1. the array flag is []:
 
# Include <stdio. h> int main (void) {int Nums [3]; Nums [0] = 11; Nums [1] = 22; Nums [2] = 33; printf ("% d, % d, % d", Nums [0], Nums [1], Nums [2]); getchar (); Return 0 ;}

  

2. array size and dimension:

 
# Include <stdio. h> int main (void) {int Nums [10]; printf ("the array size is % d \ n", sizeof (Nums); printf ("the array dimension is: % d \ n ", sizeof (Nums)/sizeof (Nums [0]); getchar (); Return 0 ;}

  

3. traverse the array:

 
# Include <stdio. h> int main (void) {int Nums [10]; int I; for (I = 0; I

For the string array, we can also think like this...

# Include <stdio. h> int main (void) {char CS [] = "abcdefg"; int I; for (I = 0; CS [I]; I ++) {/* When Cs [I] is false, the empty character */printf ("% C \ n", CS [I]) is reached;} getchar (); return 0 ;}

  

4. For a one-dimensional array, the dimension can be automatically recognized without being specified:

 
# Include <stdio. h> int main (void) {double Ds [] = {1.1, 2.2, 3.3, 4.4}; int COUNT = sizeof ds/sizeof DS [0]; int I; for (I = 0; I

4. Arrays with no dimension specified are often used as strings:

 
# Include <stdio. h> int main (void) {char str1 [] = "Builder";/* initialize */Char str2 [8] = {'B ', 'U', 'I', 'l', 'D', 'E', 'E', 'R', '\ 0'}; unsigned I; for (I = 0; I

5. Two-dimensional array:

# Include <stdio. h> int main (void) {int Nums [3] [4] ={{,}, {, 22 }}; printf ("% d, % d, % d \ n", Nums [0] [2], Nums [1] [2], Nums [2] [2]); getchar (); Return 0 ;}

  

5. The first dimension can only be omitted:

 
# Include <stdio. h> int main (void) {int Nums [] [4] ={{,}, {, 22 }}; printf ("% d, % d, % d \ n", Nums [0] [2], Nums [1] [2], Nums [2] [2]); getchar (); Return 0 ;}

  

6. Multi-dimensional array:

# Include <stdio. h> int main (void) {int Nums [2] [3] [4] ={{ 111,112,113,114 },{ 121,122,123,124 },{ 131,132,133,134 },{ 211,212,213,214 }, {221,222,223,224 },{ 231,232,233,234 }}; printf ("% d, % d, % d \ n", Nums [0] [0] [0], nums [1] [1] [1], Nums [1] [2] [2]); getchar (); Return 0 ;}

  

7. Multi-dimensional arrays can also be omitted only from the first dimension:

# Include <stdio. h> int main (void) {int Nums [] [3] [4] ={{ 111,112,113,114 },{ 121,122,123,124 },{ 131,132,133,134 },{ 211,212,213,214 }, {221,222,223,224 },{ 231,232,233,234 }}; printf ("% d, % d, % d \ n", Nums [0] [0] [0], nums [1] [1] [1], Nums [1] [2] [2]); getchar (); Return 0 ;}

  

8. String Array:

 
# Include <stdio. h> int main (void) {char CSS [] [10] = {"AAA", "BBB", "ccccccc"}; size_t I; for (I = 0; I

9. Local array variables that are not initialized include a bunch of junk values:

 
# Include <stdio. h> int NS1 [10];/* This will be initialized as null */INT main (void) {int NS2./* this will not be initialized */int I; for (I = 0; I

10. It is easy to initialize an array to be empty:

 
# Include <stdio. h> int main (void) {int NS1 [10] = {null }; /* or {0} */INT NS2. [2] [3] [4] = {0}; int I; for (I = 0; I

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.