C language 23-typedef

Source: Internet
Author: User
Tags aliases

    • I. Introduction to the Role of typedef
    • II. typedef and Pointers
    • III. typedef and structural bodies
    • Third, typedef and pointers to the structure body
    • IV. typedef and enumeration types
    • V. typedef and pointers to pointers to functions
    • Six, typedef and # define

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

This introduces a keyword that is very common in C language---typedef.

I. Introduction to the Role of typedef

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

1 #include <stdio.h> 2  3 typedef int INTEGER 4 typedef unsigned int uinterger; 5  6 typedef float float; 7
   8 int main (int argc, const char * argv[]) {9     Integer i = -10;10     Uinterger UI = 11;11     -     Float f = 12.3 9f;13     printf ("%d  %d  %.2f", I, UI, f);     return 0;17}

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

1//define a struct 2 struct MyPoint {3     float x; 4     float y; 5}; 6  7 int main (int argc, const char * argv[]) {8< c13/>//defining struct Variables 9     struct MyPoint p;10     p.x = 10.0f;11     p.y = 20.0f;12     return 0;14}

By default, we define struct variables with a struct keyword to see line 9th.

2. Using typedef to alias structs

1//define a struct 2 struct MyPoint {3     float x; 4     float y; 5}; 6  7//alias 8 typedef struct MYPOINT point; 9 int Main (int argc, const char * argv[]) {One-to-one     //define struct-body variable,     p;13     p.x = 10.0f;14     p.y = 20.0f;15     Return 0;17}

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.

1 #include <stdio.h> 2  3//define a struct and alias 4 typedef struct {5     float x; 6     float y; 7} point; 8  9//Up Alias ten typedef point *PP;11 int Main (int argc, const char * argv[]) {     //definition struct variable     />16     //define pointer variable     p = &point;18     //Use pointer variable to access struct member-     printf ("x=%f,y=%f", P->x, p-> y);     return 0;22}

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.

1//define enum type 2 enum Season {Spring, summer, autumn, winter}; 3//alias for enum type 4 typedef enum Season Season; 5  6 int main (int argc, const char * argv[]) {7     //Definition Enumeration variable 8     Season s = Spring; 9     return 0;11}

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;

V. typedef and pointers to pointers to functions

1. Review the knowledge of the function pointers first

1 #include <stdio.h> 2  3//define a SUM function that calculates a and B and 4 int sum (int a, int b) {5     int c = a + B; 6     printf ("%d + %d =%d ", A, B, c); 7     return C; 8} 9 int main (int argc, const char * argv[]) {     one///define a pointer variable p12     int (*p) (int, int) = s for the SUM function um;13     //Use the pointer variable p to invoke the SUM function (     *p) (4, 5),     and     return 0;18}

* 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

1 #include <stdio.h> 2  3//define a SUM function that calculates a and B and 4 int sum (int a, int b) {5     int c = a + B; 6     printf ("%d + %d =%d ", A, B, c); 7     return C; 8} 9 typedef int (*mysum) (int, int), one int main (int argc, const char * argv[]) {     //define a pointer to sum function pointer variable p14     MySum p = sum;15     //Use pointer variable p to call the Sum function (     *p) (4, 5);     return 0;20}

* 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

1 typedef char *STRING;2 3 int main (int argc, const char * argv[]) {4     String str = "This is a string!"; 5     return 0;6}

* 2nd paragraph

1 #define String char * * 3 int main (int argc, const char * argv[]) {4     string str = "This is a string!"; 5     return 0;6}

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

1 typedef char *STRING1; 2  3 #define STRING2 char * 4  5 int main (int argc, const char * argv[]) {6     String1 str1, str2; 7      8     Str Ing2 STR3, STR4; 9     return 0;10}

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, it is better to use typedef instead of # define when you alias the type later.

C language 23-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.