Indispensable Windows Native (5), windowsnative
[Download source code]
Indispensable Windows Native (5)-C language: Array
Author: webabcd
Introduction
Indispensable C language for Windows Native
Example
CArray. h
#ifndef _MYHEAD_ARRAY_#define _MYHEAD_ARRAY_ #ifdef __cplusplus extern "C"#endif char *demo_cArray();#endif
CArray. c
/** Array ** defines the array, int ary [10]. The description is as follows: * 1. int Is the Data Type of the data element, and 10 is the number of array elements, the system allocates memory space based on the data type and number of elements of the array element. * 2. The entire array is a continuous memory unit starting with the first address. * 3. The array name ary indicates this first address of the array, that is to say, ary is equivalent to & ary [0] */# include "pch. h "# include" cArray. h "# include" cHelper. h "char * demo_cArray () {// defines and initializes an array // int ary [] = {0, 1, 2, 3, 4 }; // define an array with a length of 10 and initialize the first five elements. Other elements are 0 by default. // Note: constants must be used to define the length of the array, cannot Use variables (Note: ANSI C99 Allows defining the size of a local array with variables) int ary [10] = {0, 1, 2, 3, 4}; // modify the value of an element in the array) ary [8] = 0; // sizeof (ary)-memory space occupied by array ary // because ary is an integer array, each integer data occupies sizeof (int) the number of array elements is sizeof (ary)/sizeof (int) int count = sizeof (ary)/sizeof (int); for (int I = 0; I <count; I ++) {int x = ary [I];} // specify the number of rows (one-dimensional length) and the number of columns (two-dimensional length) to define a two-dimensional array, and initialize int ary2 [3] [2] = {1, 2}, {3, 4}, {5, 6 }}; // modify the value of an element in a two-dimensional array. ary2 [0] [1] = 2; // when initializing a two-dimensional array, you can also write data in sequence in one row, as shown below. The result is the same as above // int ary2 [3] [2] = {1, 2, 3, 4, 5, 6}; // when defining a two-dimensional array, the number of rows (one-dimensional length) can be omitted, as shown below. The result is the same as above // int ary2 [] [2] = {1, 2, 3, 4, 5, 6}; // traverse the two-dimensional array for (int I = 0; I <3; I ++) {for (int j = 0; j <2; j ++) {int x = ary2 [I] [j] ;}} // define a character array and initialize // char aryStr [] = {'I', '', 'A', 'M','', 'w ', 'E', 'B', 'A', 'B', 'B', 'C', 'D'}; // The length is 12, and the Occupied Space is 12. // define an array of characters, and initialize it as a string // char aryStr [] = {"I am webabcd"}; same as char aryStr [] = "I am webabcd"; // length 12, space occupation 13 // when assigned to the character array in the form of a string, an additional word is automatically added at the end String Terminator, that is, '\ 0', used to mark the end of a string (the end identifier of a string is' \ 0', that is, 0 in the ASCII code, that is, NULL) char aryStr [] = "I am webabcd"; // String length int length = strlen (aryStr); // 12 // memory space occupied by the string (if aryStr is assigned to others, when passed as a parameter, it is represented as the first address of the array. If sizeof is used, the size of the memory space occupied by the pointer is obtained) int memory = sizeof (aryStr); // 13/** Note: * 1. Here, sizeof (aryStr) obtains the space occupied by the array, and strlen (aryStr) the array length is obtained * 2. However, as we have said before, the array name aryStr is the first address of the array, the entire array is a continuous memory unit starting with the first address * 3. RyStr is assigned to others (such as char * s = aryStr;), or passed as a parameter, it is represented as the first address of the array. For example, in the corresponding form parameter, if sizeof (ary) is used, the size of the memory space occupied by the pointer is obtained. * // ** in addition: * 1. If you know the first address of a string, you will know the length of the string, because it will end with "0" * 2. If you know the first address of a normal array, you do not know the length of this array, because you don't know where it ends * // write it to an array, use "bubble sort" for example. // remember to go to a company C # For a written test, the question is to sort an array. I wrote/* int [] ary = {1, 9, 5, 6, 3}; ary = ary. orderBy (p => p ). toArray (); * // result the interviewer says this is not correct... void bubble_sort (int ary [], int length); int ary_int [] = {14, 80, 19, 6, 26, 2}; bubble_sort (ary_int, 6 ); // 2, 6, 14, 19, 26, 80 return str_concat2 (int_toString (length), int_toString (memory ));} // Bubble Sorting Algorithm void bubble_sort (int ary [], int length) {for (int I = 0; I <length-1; I ++) {bool sorted = true; for (int j = 0; j <length-1-I; j ++) {if (ary [j]> ary [j + 1]) {sorted = false; int temp = ary [j]; ary [j] = ary [j + 1]; ary [j + 1] = temp ;}} if (sorted) break ;}} /* write to the for loop and find a thing for (int I = 0;) which is legal under C99 and the previous standard for (int I = 0 ;;) it is invalid (the variable cannot be declared in the Statement). You need to write int I; for (I = 0 ;;);*/