struct and typedef struct

Source: Internet
Author: User

struct and typedef struct

Divided into three pieces to tell:
1 First://Note different in C and C + +
To define a struct type in C, use a typedef:
     typedef struct STUDENT
{
int A;
}stu;
So when declaring a variable, you can: Stu stu1; (if there is no TypeDef, you must use struct Student stu1; declare)
The Stu here is actually the alias of the struct student.stu==struct Student
In addition here can also not write Student (so also cannot struct Student stu1;, must be Stu stu1;)
     typedef struct
{
int A;
}stu;
But in C + + It's simple, directly
     struct Student
{
int A;
};     
Therefore, the structure type student is defined, and the variables are declared directly student stu2.
======================================================================================

2. Second:
If you use typedef in C + +, it will make a difference:
     struct Student
{
int A;
}stu1;//STU1 is a variable


    typedef struct STUDENT2
{
int A;
}STU2; STU2 is a struct type =struct Student


Direct access to stu1.a when used
But the STU2 must first STU2 S2;
Then s2.a=10;
======================================================================================

3 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:
    typedef 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
typedef 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.

Not quite understood here.

    The difference between typedef struct and struct:

    typedef struct TAGMYSTRUCT
{
int iNum;
Long llength;
} mystruct;

The tagmystruct above is an identifier, and mystruct is the variable type (equal to (Int,char, etc.)).

This statement actually accomplishes two things:

1) Define a new type of structure

   struct TAGMYSTRUCT
{
int iNum;
Long llength;
};

Analysis: Tagmystruct is called "tag", that is, "tag", is actually a temporary name, whether or not there is typedefstruct keyword and tagmystruct together, constitute this structure type, this structure exists.

We can use struct tagmystruct varname to define variables, but it is important to note that it is wrong to use tagmystruct varname to define variables, because structs and tagmystruct together can represent a struct type.

2) typedef has a name for this new structure, called MyStruct.

typedef struct TAGMYSTRUCT mystruct;

Therefore, MyStruct is actually equivalent to struct tagmystruct, and we can use MyStruct varname to define variables.

2.

    typedef struct TAGMYSTRUCT
{
int iNum;
Long llength;
} mystruct;

In C, there are two ways to apply structural variables after this declaration:

(1) struct TAGMYSTRUCT variable name

(2) MyStruct variable name

In C + +, you can have

(1) struct TAGMYSTRUCT variable name

(2) MyStruct variable name

(3) tagmystruct variable name

struct and typedef struct

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.