[Learning notes] [C Language] typedef, learning notes typedef

Source: Internet
Author: User

[Learning notes] [C Language] typedef, learning notes typedef

1. Concept

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

2. Role: Create a new name for an existing type

3. Application scenarios:
1> Basic Data Type
2> pointer
3> struct
4> Enumeration
5> pointer to function

4. Code

1 # include <stdio. h> 2 3 typedef int MyInt; 4 typedef MyInt MyInt2; 5 6 // a new type name String 7 typedef char * String for the pointer type char; 8 9/* 10 struct Student 11 {12 int age; 13}; 14 typedef struct Student MyStu; 15 */16 17/* 18 typedef struct Student 19 {20 int age; 21} MyStu; 22 */23 24 25 typedef struct 26 {27 int age; 28} MyStu; 29 30/* 31 enum Sex {Man, Woman }; 32 typedef enum Sex MySex; 33 */34 35 typedef enum {36 Man, 37 Woman 38} MySex; 39 40 41 typedef int (* MyPoint) (int, int ); 42 43 int minus (int a, int B) 44 {45 return a-B; 46} 47 48 int sum (int a, int B) 49 {50 return a + B; 51} 52/* 53 struct Person 54 {55 int age; 56}; 57 58 typedef struct Person * PersonPoint; 59 */60 61 typedef struct Person 62 {63 int age; 64} * PersonPoint; 65 66 int main () 67 {68 // define struct variable 69 struct Person p = {20}; 70 71 PersonPoint p2 = & p; 72 73 // struct Person * p2 = & p; 74 75 // MyPoint p = sum; 76 // MyPoint p2 = minus; 77 // int (* p) (int, int) = sum; 78 79 // int (* p2) (int, int) = minus; 80 81 // p (10, 11 ); 82 83 84 // MySex s = Man; 85 // enum Sex s = Man; 86 // enum Sex s2 = Woman; 87 88 // struct Student stu3; 89 // MyStu stu = {20}; 90 // MyStu stu2 = {21}; 91 92 return 0; 93} 94 95 void test2 () 96 {97 String name = "jack"; 98 99 printf ("% s \ n", name); 100} 101 102 void test () 103 {104 int; 105 MyInt I = 10; 106 MyInt2 c = 20; 107 108 MyInt b1, b2; 109 110 printf ("c is % d \ n", c); 111}

Usage notes

1 # include <stdio. h> 2 3 // # define Integer int 4 5 // typedef int Integer; 6 7 // typedef unsigned long int MyInt; 8 9 # define String2 char * 10 11 typedef char * String; 12 13 int main () 14 {15/* 16 int a, B; 17 int a; 18 int B; 19 */20 21 // s1 and s2 are char * pointers 22 String s1, s2; 23/* 24 String s1; 25 String s2; 26 */27 s1 = "jack"; 28 s2 = "rose"; 29 30 // s3 is the char * pointer, s4 is only char31 String2 s3, s4; 32/* 33 char * s3, s4; 34 char * s3; 35 char s4; 36 */37 // String2 s3 = "jake"; 38 39/* 40 String s1; 41 String s2; 42 */43 44 45 46 47 // Integer I = 10; 48 49 return 0; 50}

 

 

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.