20-c language typedef

Source: Internet
Author: User
Tags aliases

I. Introduction to the Role of typedef

* We can use the TypeDef keyword to define a new name (alias) for various data types.

#include <stdio.h>typedefintinteger;typedef unsignedintUinterger;typedeffloatFloat;intMainintargcConst Char*argv[]) {Integer I= -Ten; Uinterger UI= One; Float F=12.39f; printf ("%d%d%.2f", I, UI, F); return 0;}
The 3rd, 4th, and 6th lines give an alias to int, unsigned int, float, and then use an alias in the main function to define the variable, which is exactly the same as the original primitive type. Output Result:

Of course, the original int, float can still be used normally after giving the type an alias:

int i = 10;float f = 10.0f;

* You can also alias an alias based on the

typedef int INTEGER;TYPEDEF Integer Myinteger;

II. typedef and Pointers

A typedef can alias a pointer, except that it can alias a basic data type

1 #include <stdio.h> 2  3 typedef char *string; 4  5 int main (int argc, const char * argv[]) {6     //equivalent to Cha R *STR = "This is a string!"; 7     String str = "This is a string!"; 8      9     printf ("%s", str);     return 0;12}

Is it a bit of a Java sense to alias a string to the pointer type char * in the 3rd, and then to use string in line 7th to define the strings?

III. typedef and structural bodies

Alias the struct to make the code more concise 1. Use of struct variables by default

 #include <stdio.h>typedef  char  *string;  int  Main (int  argc, const  char  * argv[]) { //     String str =  " this is a string!   "        ; printf (  " %s   "         return  0  ;}  
By default, we define struct variables with a struct keyword to see line 9th 2. Use typedef to alias structs

//define a struct bodystructMyPoint {floatx; floaty;};intMainintargcConst Char*argv[]) {    //defining struct-body variables    structMyPoint p; P.x=10.0f; P.Y=20.0f; return 0;}
In line 8th we gave the struct mypoint an alias called point, and then we defined a struct variable p with point 12 in line, no more struct keywords.

In fact, the code in line 8th of 1~ can be abbreviated as:

Define a struct, by the way alias typedef struct MYPOINT {    float x;    float y;} Point;

You can even omit struct names:

typedef struct {    float x;    float y;} Point;

Third, typedef and pointers to the structure body

typedef can alias pointers, structs, and, of course, pointers to struct bodies.

#include <stdio.h>//define a struct and aliastypedefstruct {    floatx; floaty;} Point;//alias fromtypedef point *PP;intMainintargcConst Char*argv[]) {    //defining struct-body variablesPoint point = {Ten, -}; //Defining pointer VariablesPP p = &Point ; //accessing struct members with pointer variablesprintf"x=%f,y=%f", P->x, p->y); return 0;}
In line 4th, a struct is defined, and a pointer to the struct is defined by the individual named point, and the 10th behavior defines the alias pp. The 2 aliases are then used in the main function.

Output Result:

IV. typedef and enumeration types

Using typedef to alias an enumeration type can also make the code concise.

// defining enum Types enum Season {Spring, summer, autumn, winter}; // to alias an enumeration type enum Season Season; int Main (intconstChar * argv[]) {    //  Define enumeration variable     Season s = spring;         return 0 ;}
The enumeration type is defined on line 2nd, the alias is season on line 4th, and the enumeration variable is defined directly in line 8th using the alias, no longer with the enum keyword.

Line 1th ~ 4th lines of code can be simplified to:

Defines an enumeration type, and aliases typedef enum SEASON {Spring, summer, autumn, winter} Season

You can even omit the enumeration name and simplify it to:

typedef enum {Spring, summer, autumn, winter} Season;

Back to top v. typedef and pointers to functions

1. Review the knowledge of the function pointers first

#include <stdio.h>//define a sum function to calculate a and B's andintSumintAintb) {intc = A +b; printf ("%d +%d =%d", A, b, c); returnC;}intMainintargcConst Char*argv[]) {    //defines a pointer variable p that points to the SUM function    int(*p) (int,int) =sum; //Call the SUM function with the pointer variable p(*p) (4,5); return 0;}
* In line 4th defines a sum function, and line 12th defines a pointer variable p that points to the SUM function, it can be found that the pointer variable p is more complex than the general pointer variable, and is not conducive to understanding.

* Line 15th calls the SUM function that P points to, and outputs the result:

2. To simplify the code and make it easier to understand, we can use typedef to alias pointer types pointing to functions

#include <stdio.h>//define a sum function to calculate a and B's andintSumintAintb) {intc = A +b; printf ("%d +%d =%d", A, b, c); returnC;} typedefint(*mysum) (int,int);intMainintargcConst Char*argv[]) {    //defines a pointer variable p that points to the SUM functionMySum p =sum; //Call the SUM function with the pointer variable p(*p) (4,5); return 0;}
* See line 10th, meaning: to the pointer to the function of the type, an individual named MySum, the function is directed to receive 2 int type parameters, the return value is the int type.

* In line 14th, use alias mysum to define a pointer variable p that points to the SUM function, which looks much simpler and more comfortable. The function call on line 17th is the same. Six, typedef and # define

1. Let's see what the difference is between the following two sections (note the 1th line of code for each paragraph)

* 1th Paragraph

Char *String; int Main (intconstChar * argv[]) {    "This isa string! " ;     return 0 ;}
* 2nd paragraph

#define String char *int main (intconstChar * argv[]) {    "This is A string! " ;     return 0 ;}
The above two code is only the 1th line of code is different, the effect is the same: defined a string "This is a string!".

But the way they are implemented is not the same:

    • The 1th code is a typedef to char * defines the alias string
    • The 2nd code uses char * Instead of the macro name in the code string

Just looking at the two pieces of code above, it doesn't seem to be the difference between TypeDef and # define.

2. Another look at the code

Char *String1; #define String2 char *int main (intconstChar * argv[]) {    String1 str1, str2;        String2 Str3, STR4;     return 0 ;}
The 1th line gives char * an alias String1, and the 2nd line defines the macro String2. Then 4 variables are defined on lines 6th and 8th.

The point is, note: In this case, only str1, str2, STR3 are pointer variables that point to the char type, and STR4 is just a variable of type char.

Here's a brief analysis of why:

* If a variable of two int is declared consecutively, we can write this:

int A, B;

The above code is equivalent to:

int A;int b;

* etc.

1 typedef char *STRING1;2     3 String1 str1, str2;

After typedef processing, STRING1 is also considered a data type, so the 3rd line of code is equivalent to

1 String1 str1;2 String1 str2;

Since String1 is char *, the above two lines of code are equal to

Char *str1;char *str2;

* and look at the macro definition

1 #define STRING2 char * 3 String2 STR3, STR4;

Because the macro definition is purely a string substitution, with char * instead of STRING2, the 3rd line of code is equivalent to

char * STR3, STR4;

In fact, it is equivalent to:

char * Str3;char STR4;

As you can see, only STR4 are basic data types, STR1, str2, and STR3 are pointer types.

Therefore, to alias the type later, it is better to use typedef instead of using # define

20-c language typedef

Related Article

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.