Use the typedef statement to define the array type

Source: Internet
Author: User

1. Definition Format of one-dimensional array type
Typedef <element type keyword> <array type Name> [<constant expression>];
For example:
(1) typedef int vector [10];
(2) typedef char strings [80];
(3) typedef short int array [N];
The first statementDefines an array type vector whose element type is int and contains 10 elements. If typedef reserved words are not used, the elements are defined as arrays, it only defines an array vector whose element type is int and contains 10 elements. These two definitions have essential differences. If we define an array vector, the system will allocate a storage unit that stores 10 integers, 40 bytes in total; if a vector of the array type is defined, the system only registers the relevant information of this type and will use this type to define objects in the future. Specifically, it is used to register the element type int, type length 10, type name VEC, and so on of the vector, which will be used to define objects of the vector type later.
Second statementAn array strings with an element type of char and 80 elements is defined. You can use the strings type to define an array object. The elements of each array object are char, the array length (number of elements) is 80.
Statement 3Defines an array containing n elements (N is a defined symbol constant) whose element type is short Int. It can be used to directly define objects of this type, it is an array containing n short integer elements.
The following are examples of defining objects using the above types.
(1) vector V1, V2;
(2) strings S1, S2 = "define type ";
(3) array A = {25, 36, 19,48, 44,50}; // assume that the constant N is greater than or equal to 6.
The first statementTwo objects V1 and V2 of the vector type are defined. Each object is an array of the vector type, and each array is composed of 10 integer elements.
Second statementThree strings objects S1, S2, and S3 are defined, and S3 is initialized. Each object is an array containing 80 characters.
Statement 3Defines an array-type object A, which is an array containing n short integer elements. This statement also initializes array a so that a [0] ~ The element values of a [5] are 25, 36, 19,48, 44, and 50 in sequence.
2. Definition Format of two-dimensional array type
Typedef <element type keyword> <array type Name> [<constant expression 1>] [<constant expression 2>];
For example:
(1) typedef int matrix [5] [5];
(2) typedef char nametable [10] [NN];
(3) typedef double dd [M + 1] [n + 1];
The first statementDefines the array type matrix containing 25 int elements in five rows and five columns
Second statementThe array type nametable of 10 NN columns containing 10 NN char elements is defined.
Statement 3Defines the array type dd that contains m + 1 rows of N + 1 columns (m + 1) * (n + 1) Double Type elements.
The three two-dimensional array types can be used to directly define the corresponding two-dimensional array. For example:
(1) matrix MX ={{ 0 }};
(2) nametable Nt = {""}; // or use equivalent {'\ 0'} for initialization
(3) dd ={{ 0.0 }};
The first statementDefines an object MX of a matrix of the Two-dimensional integer array type. This object is a 5*5 Two-Dimensional integer array, and each element is initialized to 0.
Second statementDefines a two-dimensional character array nt of the Two-dimensional character array type nametable, each element in the array is initialized as a NULL Character
Statement 3Defines an array dd of the Two-dimensional double-precision array type, and each element of it is initialized to 0.0.
In a typedef statement, <element type keyword> can be any data type pre-defined in C ++ or any data type that you have defined earlier, therefore, the type defined by this statement can also be used in the subsequent typedef statement. For example:
(1) typedef vector vectorset [20];
(2) vectorset;
The first statementDefines an array vectorset whose element type is vector and whose number is 20
Second statementDefines an object vs whose data type is vectorset. This object contains 20 elements of the vector type, and each element contains 10 elements of the int type, therefore, the entire array contains 20 rows, 10 columns, and 200 integer elements. It is equivalent to the following definition of:
Int vs [20] [10];
The typedef statement can also be used to define an array type with a higher dimension. We will not discuss it here.
3. Define aliases for existing types
The typedef statement can not only define the array type, but also define another type name for the existing type as an alias of the original type. For example:
(1) typedef int indata;
(2) typedef char chdata;
(3) typedef char * chpointer;
The first statementAn alias indata is defined for the int type.
Second statementA chdata alias is defined for the char type.
Statement 3A chpointer alias is defined for the char * type (which is a character pointer type.
In the future, using indata, chdata, and chpointer will define corresponding objects just like using int, Char, and char * respectively. For example:
(1) indata X, Y;
(2) indata A [5] = {1, 2, 3, 4, 5 };
(3) chdata b1, b2 = 'a ';
(4) chdata C [10] = "char data ";
(5) chpointer p = 0;
The first statementTwo indata (INT) variables X and Y are defined.
Second statementDefines and initializes a one-dimensional array a [5] With the element type of Int.
Statement 3Two chdata (char) variables B1 and B2 are defined, and B2 is initialized as 'A'
Statement 4A character array C [10] is defined and initialized as "char data"
Fifth statementDefines a character pointer Variable P and initializes it to 0 (that is, null ).

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.