Beginners in the use or encounter typedef often difficult to understand its meaning, and at a loss, in the textbook has little reference value, in this from the network to the TypeDef search some information and collation to a more complete TypeDef use method for your reference.
One, array
#include <stdio.h>
#include <iostream.h>
/* Avoid the difference between Visual C for and standard for */
#define FOR if (0); else for
/* Dim (A) is used to calculate the dimensions of a, but only the dimensions of the array can be computed, and the dimensions of the pointer cannot be calculated */
#define Dim (A) (sizeof (a)/sizeof (A[0]))
/* N1 to N4 is a few constants, defined as enumerations */
enum {N1 = 2, N2 = 3, N3 = 4, N4 = 5};
/* This C programmer knows that datatype is defined as an int, which is easy to expand */
typedef int DATATYPE;
/* Define a one-dimensional array, the element dimension integer value of the array */
typedef DataType ARR1[N4];
/* Define a one-dimensional array, an array of element dimension ARR1, but ARR1 is an array, so
* ARR2 is actually a matrix
*/
typedef ARR1 ARR2[N3]; /* Here is fully equivalent to typedef int arr2[n3][n4];*/
/* As explained by ARR2, ARR3 is also a one-dimensional array, but the type of the array element is the type of ARR2
* All ARR3 are a three-dimensional array
*/
typedef ARR2 ARR3[N2]; /* Here is fully equivalent to typedef int arr3[n2][n3][n4];*/
/* Define three variables A, B, c/* with the defined ARR1,ARR2,ARR3 respectively
ARR1 A; /* Here is exactly equivalent to: int a[n4]; */
ARR2 b; /* Here is exactly equivalent to: int b[n3][n4]; */
ARR3 C; /* Here is exactly equivalent to: int c[n2][n3][n4]; */
/* The following function gives you an example of how A,b,c uses */
void Exam_1 ()
{
for (int i=0; I<dim (a); i++) a[i] = i+1;
for (int i=0; I<dim (b), i++) for (int j=0; J<dim (B[0]); j + +)
B[I][J] = (i+1) *10 + (j+1);
for (int i=0; I<dim (c), i++) for (int j=0; J<dim (C[0]); j + +)
for (int k=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 (int i=0; I<dim (a); i++) printf ("%4d", A[i]);
printf ("/n");
printf ("/nthe B is:/n");
for (int i=0; I<dim (b); i++)
{
for (int j=0; J<dim (B[0]); j + +) printf ("%4d", B[i][j]);
printf ("/n");
}
printf ("/nthe c is:/n");
for (int i=0; I<dim (c); i++)
{
for (int j=0; J<dim (C[0]); j + +)
{
for (int k=0; K<dim (c[0][0]); k++) printf ("%4d", C[i][j][k]);
printf ("/n");
}
printf ("/n");
}
}
/* The following function shows you how arrays are arranged 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<sizeof (a)/sizeof (DataType); i++) printf ("%4d", Pn[i]);
printf ("/n");
PN = (int *) b; /* equivalent to PN = &b[0][0]; */
printf ("/nthe B is:/n");
for (int i=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 (int i=0; i<sizeof (c)/sizeof (DataType); i++) printf ("%4d", Pn[i]);
printf ("/n");
}
int main (int argc, char* argv[])
{
Exam_1 ();
Exam_2 ();
return 0;
}
#define S (s) printf ("%s/n", #s); S
typedef struct _ts1{
int x, y;
} TS1, *pts1, ***pppts1; TS1 is the name of the struct, and PTS1 is the name of the struct pointer
That is, the struct struct _ts1 is named TS1,
Name the struct _ts1 * PTS1
name struct _TS1 * * * as PPPTS1
The struct description behind the typedef struct {//struct can also be removed
int x, y;
} TS2, *pts2;
typedef PTS1 *PPTS1; Define PPTS1 is a pointer to PTS1
typedef struct _tts1{
typedef struct ITTS1 {
int x, y;
} Iner;
Iner i;
int x, y;
} TTS1;
Two structural bodies
Structures within the structure can also be defined as
typedef TTS1::ITTS1 ITS1;
void Test_struct ()
{
Use of basic structure weight definitions
TS1 ts1 = {100, 200};
PTS1 pts1 = &ts1; Fully equivalent to ts1* pts1 = &ts1;
PPTS1 ppts1 = &pts1; Fully equivalent to ts1** ppts1 = &pts1;
PPPTS1 pppts1 = &ppts1; Fully equivalent to ts1*** pppts1 = &ppts1;
TS2 ts2 = {99, 88};
PTS2 pts2 = &ts2; Fully equivalent to ts2* pts2 = &ts2;
TTS1 Itts1 = {{110, 220}, 10, 20};
its1* rits1 = &itts1.i;
its1* &its1 = rits1; Equivalent to Tts1::itts1 *its1 = & (ITTS1.I);
printf ("ts1/t = (%d,%d)/n*pts1/t = (%d,%d)/n"
"**ppts1/t = (%d,%d)/n***pppts1= (%d,%d)/n/n",
ts1.x, Ts1.y, Pts1->x, Pts1->y,
(**PPTS1). x, (**ppts1). Y, (***PPPTS1). x, (***pppts1). Y);
printf ("ts2/t = (%d,%d)/n*pts2/t = (%d,%d)/n/n",
ts2.x, Ts2.y, Pts2->x, pts2->y);
printf ("itts1/t = [(%d,%d),%d,%d]/n*its1/t = (%d,%d)/n/n",
itts1.i.x, ITTS1.I.Y, itts1.x, Itts1.y, Its1->x, its1->y);
S (pts1->x = 119);
S (pts2->y = 911);
S (its1->x = 999);
printf ("ts1/t = (%d,%d)/n*pts1/t = (%d,%d)/n"
"**ppts1/t = (%d,%d)/n***pppts1= (%d,%d)/n/n",
ts1.x, Ts1.y, Pts1->x, Pts1->y,
(**PPTS1). x, (**ppts1). Y, (***PPPTS1). x, (***pppts1). Y);
printf ("ts2/t = (%d,%d)/n*pts2/t = (%d,%d)/n/n",
ts2.x, Ts2.y, Pts2->x, pts2->y);
printf ("itts1/t = [(%d,%d),%d,%d]/n*its1/t = (%d,%d)/n/n",
itts1.i.x, ITTS1.I.Y, itts1.x, Itts1.y, Its1->x, its1->y);
S ((*ppts1)->y =-9999);
printf ("ts1/t = (%d,%d)/n**ppts1/t = (%d,%d)/n/n",
ts1.x, Ts1.y, (*ppts1)->x, (*ppts1)->y);
S ((**pppts1)->x =-12345);
S ((***pppts1). y =-67890);
printf ("ts1/t = (%d,%d)/n*pts1/t = (%d,%d)/n"
"**ppts1/t = (%d,%d)/n***pppts1= (%d,%d)/n/n",
ts1.x, Ts1.y, Pts1->x, Pts1->y,
(**PPTS1). x, (**ppts1). Y, (***PPPTS1). x, (***pppts1). Y);
}
A pointer to a three-pointer function
In the use of TypeDef, the most troublesome point is the pointer to the function, if there is no function below, you know the definition of the following expression and how to use it.
Int (*s_calc_func (char op)) (int, int);
If you do not know, please see the following procedures, there are more detailed instructions
Define four functions
int add (int, int);
int sub (int, int);
int mul (int, int);
int div (int, int);
Define pointers to such functions
typedef int (*FP_CALC) (int, int);
I do not introduce, you can read the next line of content.
Int (*s_calc_func (char op)) (int, int);
The next line is exactly the same as the previous line,
Defines a function calc_func that returns a pointer to the corresponding calculated function based on the Operation character op
Fp_calc Calc_func (char op);
Returns the corresponding computed value according to the OP
int calc (int A, int b, char op);
int add (int a, int b)
{return a + B;}
int sub (int a, int b)
{return a-B;}
int mul (int a, int b)
{return a * b;}
int div (int a, int b)
{return B?/A:-1;}
Int (*s_calc_func (char op)) (int, int)//This function is used exactly the same as the next function, with the parameter op, not the last two shaping
{return Calc_func (OP);}
Fp_calc Calc_func (char op)//This function uses TypeDef to define the type of Fp_calc, and the general function of the method is more uniform, good understanding
{Switch (OP)
{
Case ' + ': return add;
Case '-': return sub;
Case ' * ': return mul;
Case '/': return div;
Default
return NULL;
}
return NULL;
}
int calc (int A, int b, char op)
{
Fp_calc fp = calc_func (OP); The following is a similar direct definition to a function pointer variable
The following line is an example of a pointer to a function without a typedef, trouble.
Int (*S_FP) (int, int) = S_calc_func (OP);
ASSERT (fp = = S_FP); Can assert that the two are equal.
if (FP) return FP (A, b);
else return-1;
}
void Test_fun ()
{
int a = +, B = 20;
printf ("Calc (%d,%d,%c) =%d/n", A, B, ' + ', calc (A, B, ' + '));
printf ("Calc (%d,%d,%c) =%d/n", A, B, '-', Calc (A, B, '-'));
printf ("Calc (%d,%d,%c) =%d/n", A, B, ' * ', Calc (A, B, ' * '));
printf ("Calc (%d,%d,%c) =%d/n", A, B, '/', Calc (A, B, '/'));
}
Run results
Calc (100, 20, +) = 120
Calc (100, 20,-) = 80
Calc (100, 20, *) = 2000
Calc (100, 20,/) = 5