typedef usage (for silence 21037475)

Source: Internet
Author: User
Tags assert data structures mul printf

typedef usage
typedef are everywhere, but it's not too much to really understand what a typedef does.

For beginners, looking at someone else's source of the full-flooded typedef often do not know the wrong, and reference books are very few, so give a source code for everyone to reference

Example one:
#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 is 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;
}

Example two:
#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

typedef struct
The struct description behind the {//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;

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);
}

Example three:
In the use of TypeDef, the most troublesome point is the pointer to the function, if there is no function below,
Do you know the definition of the expression below and how to use it?

Int (*s_calc_func (char op)) (int, int);

If you do not know, please see the following program, there are more detailed instructions http://blog.csdn.net/flyxx/

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/b:-1;
}

The purpose of this function is exactly the same as the next function job and invocation method,
The parameter is OP, not the last two shaping
Int (*s_calc_func (char op)) (int, int)
{
Return Calc_func (OP);
}

Fp_calc Calc_func (char op)
{
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

1. Basic explanations
A typedef is a C-language keyword that defines a new name for a data type. The data types here include the internal data type (INT,CHAR, etc.) and the custom data type (struct, etc.).
The purpose of using TypeDef in programming is generally two, one is to give the variable a new name that is easy to remember and meaning, and the other is to simplify some more complex type declarations.
As to what is so subtle about typedef, you should continue to look at the specific aspects of the problem.
2. typedef & Structure Issues
When defining a structure with the following code, the compiler reported an error.
is the C language not allowed to include pointers to its own in the structure? Please guess first, then read the following explanation:
typedef struct TAGNODE
{
Char *pitem;
Pnode Pnext;
} *pnode;
Answers and Analysis:

1, the most simple use of typedef
typedef long BYTE_4;
Give the known data type long a new name, called Byte_4.
2, typedef and structure in combination with the use
typedef struct TAGMYSTRUCT
{
int iNum;
Long llength;
} mystruct;
This statement actually accomplishes two things:
1) Define a new type of structure
struct TAGMYSTRUCT
{
int iNum;
Long llength;
};
Analysis:
Tagmystruct called "tag", or "tag", is actually a temporary name, with the struct keyword and tagmystruct, which form the structure type, whether or not there is a TypeDef, this structure exists.
We can use struct tagmystruct varname to define variables, but it is important to note that it is wrong to use tagmystruct varname to define variables, because structs and tagmystruct together can represent a struct type.
2) typedef has a name for this new structure, called MyStruct.
typedef struct TAGMYSTRUCT mystruct;
Therefore, MyStruct is actually equivalent to struct tagmystruct, and we can use MyStruct varname to define variables.

Answers and analysis
C is of course allowed to include pointers to its own structure, and we can see countless examples of the implementation of data structures such as linked lists, and the fundamental problem with this code is the application of typedef.
According to our above description can know: The process of building a new structure encountered the Pnext domain declaration, the type is pnode, to know that Pnode represents the type of the new name, then the type itself has not been established, the type of the new name also does not exist,
That means the compiler doesn't know Pnode at all.
There are several ways to solve this problem:
1),
typedef struct TAGNODE
{
Char *pitem;
struct Tagnode *pnext;
} *pnode;
2),
typedef struct TAGNODE *pnode;
struct Tagnode
{
Char *pitem;
Pnode Pnext;
};
Note: In this example, you use a typedef to give a new name to a type that is not yet fully declared. The C language compiler supports this practice.
3), Standard practice:
struct Tagnode
{
Char *pitem;
struct Tagnode *pnext;
};
typedef struct TAGNODE *pnode;

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.