Typedef uses Daquan 1 (array)

zhaozj2021-02-16  48

Typedef is everywhere, but it is not too much to use Typedef. For beginners, TypeDef that is full of TypedEf is often missed when you see someone's source code, and the reference book is very small, so give a source code, for your reference #include

#include

/ * Avoid different for Visual C for different * /

#define for if (0); Else for

/ * DIM (a) is used to calculate a dimension of A, but can only calculate the dimensions of the array, and cannot calculate the dimension of the pointer * /

#define Dim (a) (SizeOf (a) / sizeof (a [0]))

/ * N1 to N4 are several constants to define the form of enumeration * /

Enum {n1 = 2, N2 = 3, N3 = 4, N4 = 5};

/ * This C programmer knows that it is to define DataType as an int type, easy to expand * /

TypedEf int Dattype;

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

TypedEf DataType Arr1 [N4];

/ * Remail a one-dimensional array, an array element dimension Arr1, but Arr1 is another array, so

* Arr2 is actually a matrix

* /

Typedef Arr1 Arr2 [N3]; / * This is completely equivalent for typedef int Arr2 [N3] [N4]; * /

/ * According to the interpretation of Arr2, Arr3 is also a one-dimensional array, but the type of array element is the type of Arr2.

* All Arr3 is a three-dimensional array

* /

TYPEDEF ARR2 ARR3 [N2]; / * This is completely equivalent to TypedEf Int Arr3 [N2] [N3] [N4]; * /

/ * Use the defined ARR1, Arr2, Arr2, respectively, three variables A, B, C * /

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

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

Arr3 C; / * This is completely equivalent to: int C [N2] [N3] [N4]; * /

/ * How the following functions give you an example, how to use * /

Void Exam_1 ()

{

For (int i = 0; i

For (int i = 0; i

B [i] [j] = (i 1) * 10 (j 1);

For (int i = 0; i

For (int K = 0; K

Printf ("/ NTHE A IS: / N");

For (int i = 0; i

Printf ("/ n");

Printf ("/ NTHE B IS: / N");

For (int i = 0; i

{

For (int J = 0; J

}

Printf ("/ NTHE C IS: / N");

For (int i = 0; i

{

For (int J = 0; J

{

For (int K = 0; K

Printf ("/ n");

}

Printf ("/ n");

}

}

/ * The following function gives you the array of arrays in memory * /

Void Exam_2 ()

{

INT * PN = NULL;

Pn = (int *) a; / * equivalent to PN = & a [0]; * /

Printf ("/ NTHE A IS: / N");

For (int i = 0; i

Printf ("/ n");

Pn = (int *) b; / * equivalent to pn = & b [0] [0]; * /

Printf ("/ NTHE B IS: / N");

For (int i = 0; i

Printf ("/ n");

Pn = (int *) c; / * equivalent to PN = & C [0] [0] [0]; * /

Printf ("/ NTHE C IS: / N");

For (int i = 0; i

Printf ("/ n");

}

Int main (int Argc, char * argv [])

{

EXAM_1 ();

EXAM_2 ();

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-27691.html

New Post(0)