Chapter IV construction data type

Keywords: C++ DirectX

The basic data type can handle some simple data alone. For some complex data, it needs to be described with structural data types. Construction data type is a data type defined by users according to certain syntax rules. The constructed data types supported by C + + language include array, pointer, reference, class, structure, union, enumeration and so on.

An array is a collection of data of the same type. A one-dimensional array is a linear sequence that stores the same type of data. Each storage unit of the array can store one data, which is called the element of the array. Each element has a label called the subscript of the array. Use the array name and subscript to access the elements in the array. The following is the syntax for defining arrays:

< data type >   < Array name > [< constant >] [= {< initial value list >}];

int a_array[5];				// The definition name is a_array, the length is 5
a_array[0] = 1;
a_array[1] = 2;
a_array[2] = 3;
a_array[3] = 4;
a_array[4] = 5;
std::cout << a_array[1] << "\n";	// Output 2

int b_array[5] = {1,2,3,4,5};	// Define and initialize the array. It can be less but not more
b_array[1] = 66;
std::cout << b_array[1] << "\n";	// Output 66

The size of the array is a constant, not a variable, that is, the size of the array is a fixed explicit value. An array is a continuous piece of storage space in memory, and the fixed subscript counts from 0, that is, the subscript of the first element is 0, the subscript of the second element is 1, and so on. Any element in the array can be accessed separately in the following way: < array name > [< subscript >], for example: x[0] = 2; x[1]=3; All in a grammatical way.

Note: if we define that the length of the array is 10, but the subscript exceeds 10 when accessing, such as x[12] to access or operate this element, we call this situation array out of bounds. Cross boundary access to arrays may cause program errors, but the C + + compiler does not check it, but the program may crash when running!

Multidimensional arrays refer to arrays with dimension greater than 1. The most commonly used multidimensional arrays are two-dimensional and three-dimensional arrays. They are defined as follows:

int c_array[2][3];				// Define an array of 2 rows and 3 columns
c_array[0][0] = 1;
c_array[0][1] = 2;
c_array[0][2] = 3;
c_array[1][0] = 4;
c_array[1][1] = 5;
c_array[1][2] = 6;
std::cout << c_array[0][1] << "\n";	// Output 2

int d_array[2][3] = { {1,2,3},{4,5,6} };
d_array[0][1] = 99;
std::cout << d_array[0][1] << "\n";	// Output 99

Two dimensional array can be understood as "array in array", that is, each element in the array is a one-dimensional array. Computer memory is a one-dimensional linear structure. C + + adopts the row first storage rule, that is, first save the first row, then save the second row, and so on.

According to the data type classification of array elements, arrays can be divided into numerical arrays, character arrays, pointer arrays, object arrays, etc. Here we focus on character arrays. Character array is an array with data type char. The character array can be one-dimensional to store a string of characters or two-dimensional to store multiple strings. Strings in C + + end with "\ 0" (empty character with ASCII code value of 0) in the character array. Examples are as follows:

char e_array[3];
e_array[0] = 'a';
e_array[1] = 'b';
e_array[2] = 'c';
std::cout << e_array[1] << "\n";	// Output b

char f_array[3] = {'A','B','C'};
std::cout << f_array[1] << "\n";	// Output B

char g_array[] = "hello,array";
std::cout << g_array[1] << "\n";	// Output e

When storing a string with a character array, the length of the array is: the number of Western characters plus 1, and the number of Chinese characters multiplied by 2 plus 1. This is because the encoding of a western character takes up 1 byte, and the encoding of a Chinese character takes up 2 bytes. Plus 1 is used to store "\ 0" empty characters.

char g_array[] = "hello,array";
std::cout << g_array[1] << "\n";		// Output e

int g_len = sizeof(g_array) / sizeof(g_array[0]);
std::cout << g_len << "\n";			// Output array length 12

char h_array[] = "China";
int h_len = sizeof(h_array) / sizeof(h_array[0]);
std::cout << h_len << "\n";			// Output array length 5

wchar_t w_array[] = L"China";
int t_len = sizeof(w_array) / sizeof(w_array[0]);
std::cout << t_len << "\n";			// Output array length 3

Pointer type is the data type supported by C language, and C + + language still supports pointer data type. Reference type is a new data type introduced in C + +. It has the characteristics of pointer type, but it is safer than pointer. In modern high-level languages (c# and Java), reference types are supported instead of pointer types.

A pointer is a special data type whose value is a memory address. Variables described by pointer type are called pointer variables, which save the address of memory unit. In other words, you can directly access the memory space through pointer variables. Memory address is an integer. The system assigns a unique address to each memory unit. The syntax for defining pointer variables is as follows:

< data type > * < pointer variable name > [= & < variable >];

int pa = 100;					// Define a common int variable p_a
int* pb = &pa;					// Define a pointer variable p_b. Its value is the variable P_ Memory address of a
std::cout << *pb  << "\n";			// Output 100, pa and * pb are the same memory space

*An asterisk is a keyword of pointer type. While defining a pointer variable, you can assign the memory address of another variable to the pointer variable. The data type should be the same as that of the following variables. The pointer variable can be assigned NULL or 0, which is called a NULL pointer. The first memory address of an ordinary variable can be assigned to a pointer variable by using the address fetching character. Since the pointer variable stores the first address of memory, the operation of pointer variable is also the operation of memory address. The operation of pointer variables mainly includes assignment operation, indirect reference operation, self increasing and self decreasing operation, relationship operation between pointers, etc.

It should be noted that in C + + language, the array name is a pointer constant, which points to the starting position of the array. That is, we can assign the array name to a pointer variable, and the array elements can still be accessed through the pointer variable, as follows:

int i_array[5] = { 1,2,3,4,5 };
int* pi = i_array;
std::cout << *pi << std::endl;			// Output 1, equivalent to i_array[0]
std::cout << *(pi+1) << std::endl;		// Output 2, equivalent to i_array[1]

Similarly, we can also use pointers to define an array, as follows:

int* p_array = new int[5];					// Defining arrays with pointers
p_array[0] = 123;
*(p_array + 1) = 456;
std::cout << p_array[0] << std::endl;			// Output 123
std::cout << *(p_array+1) << std::endl;		// Output 456

When a pointer variable is used to point to a two-dimensional array, the value corresponding to the pointer variable is a one-dimensional array, as follows:

int j_array[2][3] = { {1,2,3},{4,5,6} };			// Define a two-dimensional array
int(*pj)[3] = j_array;						// Cannot be directly assigned to cPtr, there must be [3]
std::cout << (*pj)[0] << std::endl;			// Output 1, equivalent to j_array[0][0]
std::cout << (*(pj+1))[0] << std::endl;			// Output 4, equivalent to j_array[1][0]
std::cout << *(*(pj+1)+1) << std::endl;		// Output 5, equivalent to j_array[1][1]

This relationship between arrays and pointers. When an array is passed as a function parameter, it is passed by address. In other words, if we modify the array value in the calling method, the value of the original array will also change, which is more dangerous.

A reference is an alias, which is to give a variable another name. In the program, you can use the variable name to access the value of the variable, or you can use its alias to read or manipulate the value of the variable. Reference types are defined as follows:

< data type > & < reference variable name > = < defined variable name >;

int rx = 200;
int& ry = rx;
std::cout << ry << std::endl;	// Output 200, n is another name of m

&The symbol is used to declare the keyword of the reference type, < data type > must be exactly the same as the data type of the referenced variable. The cause variable is only assigned at the time of definition, and then the reference variable will no longer accept the assignment of another variable name.

int rz = 300;
ry = rz;					// As understood, ry should have changed and pointed to rz
std::cout << ry << std::endl;	// Output 300, this ry is rx? Or rz?

rz = 400;
std::cout << ry << std::endl;	// Output 300, indicating that ry is not rz
std::cout << rz << std::endl;	// Output 400, indicating that ry is not rz
std::cout << rx << std::endl;	// Output 300, indicating that ry is still rx

When we re initialize ry, we point to rx, indicating that ry is rx. When we use ry=rz later, we don't point ry to rz, but give the value of 300 to ry and rx, which is why after rz becomes 400, ry and rx are still 300.

A structure is a data type defined by a programmer and can accommodate many different data values. Once a structure type is declared and its data members are identified, multiple variables of that type can be created.

Use the keyword struct to declare a structure. Examples are as follows:

struct Student				// Define a student's structure
{
	int num;				// Student serial number
	char* name;			// Student name
	double score;			// Student test scores
};

Student stu1;
stu1.num = 1;
stu1.name = (char*)"richie";
stu1.score = 100;
std::cout << stu1.name << std::endl;	// Output richie

Student stu2 = { 2, (char*)"lee", 98 };
std::cout << stu2.name << std::endl;	// Output lee

An enumeration type is a collection of semantically related enumeration constants. The definition syntax is as follows:

enum color {red, gree, blue};	// Defines the Color enumeration type, where red=0, blue=1, green=2
color eye = red;				// The enumeration type (color) variable eye, whose value is red, that is, 0
std::cout << eye << std::endl;	// Output 0

enum is an enumeration type keyword, and < enumeration type name > is user-defined. An enumeration type contains mu lt iple enumeration constants. After the enumeration type is defined, < enumeration type name > becomes a new data type name, which is used like the int basic type. The value range of enumeration type variable is an integer, occupying 4 bytes.

Posted by ozman26 on Sun, 19 Sep 2021 16:49:24 -0700