Use the typedef statement to define the array type

Source: Internet
Author: User
Tags array definition

Use the typedef statement to define the array type

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 statement defines an array-type vector with an element type of int and 10 elements. If typedef is not used, the array definition is changed, 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 to register the element type int, type length 10, and type name vectoe of the vector, which will be used to define objects of the vector type later.

The second statement defines an array strings whose element type is Char and contains 80 elements. In the future, you can use the strings type to define an array object. The element of each array object is Char, the array length (number of elements) is 80.

The third statement defines an array type array containing n elements (N is a defined symbolic constant) whose element type is short Int, you can use it to 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 statement defines two objects of the vector type V1 and V2. Each object is an array of the vector type, and each array is composed of 10 integer elements.

The second statement defines three strings objects S1, S2, and S3, and initializes S3. Each object is an array containing 80 characters.

The third statement defines 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 statement defines the array type matrix containing 25 int elements in five rows and five columns, the second statement defines the array type nametable of 10 NN columns containing 10 NN char elements, the third statement defines the array type dd that contains m + 1 rows of N + 1 columns (m + 1) * (n + 1) double 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 statement defines an object MX of the 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; the second statement defines a two-dimensional character array nt of the Two-dimensional character array nametable. Each element in the array is initialized as a null character; the third statement defines an array dd of the Two-dimensional double-precision array type. Each element of the DD 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 statement defines an array vectorset whose element type is vector and whose number is 20. The second statement defines an object whose data type is vectorset, this object contains 20 elements of the vector type, and each element contains 10 int elements. 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 statement defines an alias indata for the int type, the second statement defines an alias chdata for the char type, and the third statement defines the char * type (it is a character pointer type) defines a chpointer alias. 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 statement defines two indata (INT) variables X and Y. The second statement defines a one-dimensional array a [5] With the element type int and initializes it, the third statement defines two chdata (char) variables B1 and B2, and initializes B2 as 'A ', the fourth statement defines a character array C [10] And initializes it to "char Data". The fifth statement defines 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.