3.3 typedef__ 3rd data "C and pointers"

Source: Internet
Author: User

Preface: 3 Years of work, it is time to summarize the thought of the 3 years, to build their own knowledge system, the formation of programming ideas, the basis and application of knowledge between the channel need to get through, then start it.


"C and Pointers"

Chapter III 3.3 typedef

1. C language support typedef mechanism;

2. typedef is used to define new names for various data types;

3. typedef declaration types can reduce the risk of making the statement smelly and long;

4. Use typedef instead of # define to create a new type name because the latter does not handle the pointer type correctly.


Code Specification Summary:

In the process of working, the introduction of TCP/IP protocol stack, file system, often in the introduction of these external libraries and modules, will introduce a new data type definition, resulting in a project, sometimes there will be 5, 6 completely different naming methods of the basic data type, which is very undesirable, It brings a lot of inconvenience to code maintenance and development, so the following suggestions are made:

1. The introduction of the library or module, regardless of development or maintenance, maintain the original basic data type unchanged;

2. New development module, based on a unified and unique basic data type development;

3. Based on the development of external libraries and modules, use the same and unique basic data type development.


Usage Experience Summary:

In Linux storage management, there is the following code

typedef struct {unsigned long pte_low;} pte_t;

#define PTE_VAL (x) ((x). Pte_low)


The use of this, indeed very good, using the object-oriented programming approach, brings the following benefits:

1. What happens if the code uses the following implementation method?

#define unsigned long pte_t;

This can cause a problem

unsigned long new_pte;

pte_t new = New_pte; compiler does not error, this is the hidden danger

This article is from the "mountain Ask the Boy" blog, please be sure to keep this source http://linpeng.blog.51cto.com/9779987/1685081

3.3 typedef__ 3rd data "C and pointers"

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.