struct and typedef in C language struct< reprint >

Source: Internet
Author: User

Original: http://www.nowamagic.net/librarys/veda/detail/1785

A typedef is a C-language keyword that defines a new name for a data type. The data types here include the internal data type (INT,CHAR, etc.) and the custom data type (struct, etc.).

The purpose of using TypeDef in programming is generally two, one is to give the variable a new name that is easy to remember and meaning, and the other is to simplify some more complex type declarations.

As to what is so subtle about TypeDef, look at the specifics of some of the issues that confront you.

One

First define a struct type in C to use a typedef:

struct student{    int A;} Stu;  

So when declaring variables, you can: Stu stu1;

If there is no TypeDef, it must be declared with a struct Student stu1.

The Stu here is actually the alias of the struct student.

In addition here can also not write Student (so also can't struct Student stu1;).

struct{    int A;} Stu;  

But in C + + It's simple, directly

struct student{    int A;};  

The struct type student is defined and the variable is declared directly student stu2.

Two

Secondly, if you use typedef in C + +, it will make a difference:

struct Student   {       int A;   } STU1;    //structint//        

The stu1.a can be accessed directly when used, but the STU2 must be STU2 S2 first, and then s2.a=10;

Three

Master the above two, but in the end we'll talk about a problem that doesn't matter much.

If in the C program we write:

struct  {    int num;    int age ;} AAA,BBB,CCC;    

What the hell is this?

I personally observe the understanding of the compiler (VC6), which is equivalent to

struct  {    int num;    int age ;} Aaa;typedef AAA bbb;typedef AAA CCC;    

That is to say, AAA,BBB,CCC are all structural types. Any one can be used to declare a variable, as is the case in C + +. But what you should note is that in C + + if the typedef keyword is written out, then AAA,BBB,CCC will be a distinct three object.

struct and typedef in C language struct< reprint >

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.