Detailed description of typedef Array

Source: Internet
Author: User

Typedef is everywhere, but it can really understand that typedef is not used too much. For beginners, when looking at other people's source code, typedef everywhere is often wrong, and there are few reference books, so here is a source code for your reference.

 

# Include<Stdio. h>

# Include<Iostream. h>

 

/* Avoid differences between Visual C for and standard */

# Define
For
If(0 );Else
For

 

/* Dim (a) is used to calculate the dimension of A. However, it can only calculate the dimension of an array and cannot calculate the dimension of a pointer */

# DefineDim ()(Sizeof()/Sizeof(A [0])

 

/* Several constants from N1 to N4 are defined in enumeration form */

Enum{N1 = 2, n2 = 3, N3 = 4, N4 = 5 };

 

/* This C programmer knows that datatype is defined as int type to facilitate expansion */

Typedef
IntDatatype;

 

/* Define a one-dimensional array with an element-dimension integer value */

TypedefDatatype arr1 [N4];

 

/* Define another one-dimensional array. The Element Dimension of the array is arr1, but arr1 is an array, so

* Arr2 is actually a matrix.

*/

TypedefArr1 arr2 [N3];/*
This is equivalent to typedef int arr2 [N3] [N4]. */

 

/* According to arr2, arr3 is also a one-dimensional array, but the array element type is arr2.

* All arr3 are a three-dimensional array.

*/

TypedefArr2 arr3 [n2];
/*It is equivalent to typedef int arr3 [n2] [N3] [N4]. */

 

/* Define three variables A, B, and C respectively with the defined arr1, arr2, and arr3 */

Arr1;
/*This is equivalent to: int A [N4]; */

Arr2 B;
/*This is equivalent to int B [N3] [N4]; */

Arr3 C;
/*This is equivalent to int C [n2] [N3] [N4]; */

 

/* The following function provides an example to show how to use A, B, and C */

VoidExam_1 ()

{

For(IntI = 0; I <dim (a); I ++) A [I] = I + 1;

 

For(IntI = 0; I <dim (B); I ++)
For(IntJ = 0; j <dim (B [0]); j ++)

B [I] [J] = (I + 1) * 10 + (J + 1 );

 

For(IntI = 0; I <dim (c); I ++)
For(IntJ = 0; j <dim (C [0]); j ++)

For(IntK = 0; k <dim (C [0] [0]); k ++) C [I] [J] [k]
= (I + 1) * 100 + (J + 1) * 10 + (k + 1 );

 

Printf ("\ nthe A is: \ n ");

For(IntI = 0; I <dim (a); I ++) printf ("% 4D", a [I]);

Printf ("\ n ");

 

Printf ("\ nthe B is: \ n ");

For(IntI = 0; I <dim (B); I ++)

{

For(IntJ = 0; j <dim (B [0]); j ++) printf ("% 4D
", B [I] [J]);

Printf ("\ n ");

}

 

Printf ("\ nthe C is: \ n ");

For(IntI = 0; I <dim (c); I ++)

{

For(IntJ = 0; j <dim (C [0]); j ++)

{

For(IntK = 0; k <dim (C [0] [0]);
K ++) printf ("% 4D", C [I] [J] [k]);

Printf ("\ n ");

}

Printf ("\ n ");

}

}

 

/* The following function shows the arrangement of arrays in memory */

VoidExam_2 ()

{

Int* Pn =
Null;

 

Pn = (Int*);/*
Equivalent to Pn = & A [0]; */

Printf ("\ nthe A is: \ n ");

For(IntI = 0; I <Sizeof()/Sizeof(Datatype );
I ++) printf ("% 4D", PN [I]);

Printf ("\ n ");

 

Pn = (Int*) B;/*
Equivalent to Pn = & B [0] [0]; */

Printf ("\ nthe B is: \ n ");

For(IntI = 0; I <Sizeof(B )/Sizeof(Datatype );
I ++) printf ("% 4D", PN [I]);

Printf ("\ n ");

 

Pn = (Int*) C;/*
Equivalent to Pn = & C [0] [0] [0]; */

Printf ("\ nthe C is: \ n ");

For(IntI = 0; I <Sizeof(C )/Sizeof(Datatype );
I ++) printf ("% 4D", PN [I]);

Printf ("\ n ");

}

 

 

 

IntMain (IntArgc,
Char* Argv [])

{

Exam_1 ();

Exam_2 ();

Return0;

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.