struct and typedef struct

Source: Internet
Author: User

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.typedef struct and struct differences:

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;
};

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

======================================================

4.LNode and *linklist

typedef struct LNODE
{
int data;
struct Lnode *next;
}lnode,*linklist;


Note that the struct lnode here is a whole, indicating that Lnode is a struct type, the struct Lnode is redefined as lnode, then it can be used to define struct variables, pointers, etc. directly using Lnode.

typedef struct LNODE* linklist;
Redefine the struct lnode* as linklist, and later you can use linklist to simplify defining variables for the lnode* type (of course, a pointer to a struct variable). It should be considered that the one in the definition is connected to the previous structure, not to the linklist of the latter, and this is where confusion can arise.

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.