Talk C chestnuts together (92nd back: C language example -- talking about typedef)

Source: Internet
Author: User

Talk C chestnuts together (92nd back: C language example -- talking about typedef)
Hello, everyone. The last time we talked about the array size example, this example is:Talking about typedef. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Recently, some readers have asked some questions about typedfe. I will give a special description here, hoping to help you.

Typedef is a keyword of C language,Used to redefine the type.In layman's terms, you can call an alias or nickname..

Typedef usually has two functions:

To make the program concise and easy to understand (readable); to avoid program errors;

For more information, see the following example:

typedef struct person{    int age;    char *name;    char flag;}Person;

In this example, a new type is defined: Person. It has the same effect as struct person. In the following code

Person p1; // define a struct variable struct person p1; // define a struct variable

In the preceding example, Person p1 is equivalent to struct person p1. however, the preceding method is more concise.

Do you still remember the function signal we introduced in the previous chapter? Below isSignal function prototype:

void (*signal(int signo, void (*func) (int))) (int)

We can define a new type:

typedef void func (int);

It indicates a function, which has an int type parameter and returns void. After this type is defined, the function prototype of the function signal can be written as follows:

Func * signal (int signo, func * pFun); // use typedef to define the signal function of the New Type

Is the current signal function much simpler.

Avoid errors

We also give an example to avoid program errors:

#include 
  
   int main(){    int *p1,p2;    int a,b;    a = 3;     b = 5;    p1 = &a;    p2 = &b;    printf("a = %d \n",*p1);    printf("b = %d \n",*p2);    return 0;}
  

Compile the above program with compilation warnings and errors:

In function 'main': typFile. c: 12: 5: warning: assignment makes integer from pointer without a cast [enabled by default] p2 = & B; // issue a warning typFile. c: 15: 21: error: invalid type argument of unary '*' (have 'int') printf ("B = % d \ n", * p2 ); // compilation Error

In fact, we want to define two int type pointers. The result defines a pointer and a variable. To avoid this error, we can use typedef in the program to define a pointer type. The details are as follows:

# Include
  
   
    
Typedef int * pInt; // define a new pointer type int main () {pInt p1, p2; // use the new type to define two pointers: int a, B; a = 3; B = 5; p1 = & a; p2 = & B; printf ("a = % d \ n", * p1); printf ("B = % d \ n ", * p2); return 0 ;}
   
  

Compile the program. The warning and error are missing. The compiled program can get the following results:

A = 3 B = 5 // The program runs correctly

Let's talk about the example of typedef. I want to know what examples will be provided later, and I will try again.

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.