To define an array type using typedef statements

Source: Internet
Author: User
Tags array definition

To define an array type using typedef statements

1. Defining formats for one-dimensional array types

typedef < element type keywords >< 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, containing 10 elements, and, without a typedef reserved word, becomes an array definition, which defines only an array vector with an element type of int and 10 elements. These two definitions have an essential difference, if the array vector is defined, the system allocates a storage unit with 10 integers, a total of 40 bytes of storage space, and if the array type vector is defined, the system simply registers the relevant information of that type and uses it later to define the object using that type. Specifically, the vector element type int, type length 10, type name Vectoe, etc. are registered to be used later to define the object of the vector type.

The second statement defines an element type of char, an array of type strings with 80 elements, which can be used to define an array object directly using the strings type, with the elements of each array object char, and the array length (that is, the number of elements) to 80.

The third statement defines an array of type n elements with an element type of short int (n is a defined symbolic constant), which you can then use to directly define an object of that type, which is an array of n elements of the shorter integer type.

Here are some examples of using the above types to define objects.

(1) vector v1,v2;

(2) Strings s1,s2= "define type";

(3) array a={25,36,19,48,44,50}; //assumed constant n≥6

The first statement defines two objects of the vector type V1 and v2, each of which is an array of vector types, each consisting of 10 integral elements.

The second statement defines the three objects S1,s2 and S3 of the strings type, and initializes the S3, each of which is an array of 80 character spaces.

The third statement defines an array of type Object A, which is a set of n short integer elements, which is initialized at the same time to the a[0]~a[5] element value to 25, 36,19,48,44 and 50.

2. Defining formats for two-D array types

typedef < element type keywords >< 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 an array type containing 5 rows and 5 columns with a total of 25 int elements, and the second statement defines an array type of 10 rows of nn columns with a total of 10*nn char elements nametable, and the third statement defines the containing m+1 row n +1 columns Total (m+1) * (n+1) The array type DD for the element of type double.

Using these three two-dimensional array types, the corresponding two-dimensional arrays can be defined directly. such as:

(1) Matrix mx={{0}};

(2) nameTable nt={""}; //or initialize using equivalent {{'}}}}

(3) DD dd={{0.0}};

The first statement defines a two-dimensional integer array type an object MX for the matrix, which is a 5*5 two-dimensional array of integers, each of which is initialized to 0; the second statement defines a two-dimensional character array of two-dimensional character array type NameTable NT, Each element in the array is initialized to a null character, and the third statement defines an array dd of two-dimensional double-precision array type DD, with each element initialized to 0.0.

The,< element type keyword in a typedef statement > can be any of the predefined data types in the C + + language, or any one of the data types that the user has previously defined, So the type defined by the statement can also be used in subsequent typedef statements. such as:

(1) typedef vector VECTORSET[20];

(2) Vectorset vs;

The first statement defines an array type vectorset with an element type of vector and an element number of 20, and the second statement defines an object with a data type of Vectorset VS, which contains 20 elements of type vector. Each element also contains 10 elements of type int, so the entire array contains 20 rows and 10 columns of 200 integer elements, which equates to the following definition of VS:

int vs[20][10];

It is also possible to define a higher-dimensional array type using typedef statements, which are not discussed here.

3. Define an alias for an existing type

A typedef statement can be used to define not only an array type, but also another type name for an existing type, as an alias of the original type. such as:

(1) typedef int INDATA;

(2) typedef char CHDATA;

(3) typedef char* Chpointer;

The first statement defines an alias for the int type indata, the second statement defines an alias for the char type Chdata, and the third statement defines an alias Chpointer for the char* type, which is a character pointer type. Using Indata,chdata and Chpointer in the future is like using Int,char and char* respectively to define the corresponding object. such as:

(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 the two variables x and y of the type indata (that is, int), the second statement defines a one-dimensional array with an element type of int a[5] and initializes it, and the third statement defines the two variables B1 and B2 of the Chdata (that is, char), The B2 is initialized to 'a ', the fourth statement defines a character array c[10] and initializes to "char data", and thefifth statement defines a character pointer variable p and initializes it to 0 (that is, null).

To define an array type using typedef statements

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.