Data Structure basics-Use of struct and type alias typedef, structtypedef

Source: Internet
Author: User

Data Structure basics-Use of struct and type alias typedef, structtypedef
I. Create struct

In C language, a common method to implement data structures is to use structure. The sample code is as follows:

Struct stu {int num; char ch ;};

Struct indicates to create a struct stu as the struct name, which contains various variable types (which can be nested with struct). The following is an example:

Struct stu s; scanf ("% d", & s. num); printf ("% d \ n", s. num );

The first line indicates the structure s in which the structure is created as stu. Then, the content in the access structure body needs to use the name. Name, for example, the s. num in the access structure s represents the num in the access structure s. For convenience, we will introduce the typedef keyword.

Ii. Usage of typedef

Typedef serves as an alias for the variable type. For example, long is too long. It is annoying to write long every time. We can simplify it like this.

Typedef long ll; ll B; scanf ("% lld", & B); printf ("% lld \ n", B );

So we can use struct as follows:

Typedef struct {int num; char ch;} stu; stu s; scanf ("% d", & s. num); printf ("% d \ n", s. num );

In this way, you can directly use stu to create a struct without using struct stu s; this is a long string.

 

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.