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