typedef in C + + and its combination with struct use _c language

Source: Internet
Author: User
Tags define definition int size

Copy Code code as follows:

It is equivalent to creating an alias, or type alias, for an existing type.
Plastic surgery, etc.
typedef int size;


Character array
Char line[81];
Char text[81];//=>

typedef char LINE[81];
line text, Secondline;


Pointer
typedef char * PSTR;
int mystrcmp (pstr p1, pstr p2);//Note: cannot write int mystrcmp (const pstr p1, const PSTR p3); is interpreted as char * const CP (not a simple alternative) because of const PSTR P1 )


Used in combination with struct types
typedef struct TAGMYSTRUCT
{
int inum;
Long llength;
} mystruct;//(here mystruct is a struct type alias) =>

struct TAGMYSTRUCT
{
int inum;
Long llength;
};//+
typedef struct TAGMYSTRUCT mystruct;


Structure contains pointer usage pointing to itself
typedef struct TAGNODE
{
Char *pitem;
Pnode Pnext;
} *pnode;//=>error
1)
typedef struct TAGNODE
{
Char *pitem;
struct Tagnode *pnext;
} *pnode;
2)
typedef struct TAGNODE *pnode;
struct Tagnode
{
Char *pitem;
Pnode Pnext;
};
3) Specification
struct Tagnode
{
Char *pitem;
struct Tagnode *pnext;
};
typedef struct TAGNODE *pnode;


The difference between the define and the
1)
typedef char* PSTR1;//Recreate name
#define PSTR2 Char *//simple text substitution
PSTR1 S1, S2;
PSTR2 S3, S4;=>pstr2 S3, *s4;
2) Define definition If there is an expression in the definition, parentheses, the typedef do not need.
#define F (x) x*x=> #define F (x) ((x) * (x))
Main ()
{
int a=6,b=2,c;
C=f (a)/F (b);
printf ("%d \\n", c);
}
3 typedef is not a simple text replacement
typedef char * PSTR;
Char string[4] = "ABC";
const char *P1 = string;
Const PSTR P2 = String;=>error
p1++;
p2++;

1) #define宏定义有一个特别的长处: You can use #ifdef, #ifndef等来进行逻辑判断, and you can use #undef to cancel the definition.
2 The typedef also has a special advantage: it conforms to the scope rule, and the variable type defined by the TypeDef is scoped to the defined function or file (depending on where the variable is defined), and the macro definition does not have this attribute.

Copy Code code as follows:

//
Definition of struct type in C
typedef struct STUDENT
{
int A;
}stu;//affirm variable Stu stu1 or struct Student stu1;
Or
typedef struct
{
int A;
}stu;//affirm variable Stu stu1;

defining struct types in C + +
struct Student
{
int A;
};//affirm variable student stu2;


Use differences in C + +
struct Student
{
int A;
}STU1;//STU1 is a variable. Visit stu1.a

typedef struct STUDENT2
{
int A;
}STU2;//STU2 is a structural body type that accesses STU2 S2; s2.a=10;
has yet to be increased.

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.