Structure and pointer in C Language (application in data structure)
[Note ]:
Before talking about the following things, let's talk about a few mistakes first,
1. sizeof this thing looks like a function. In fact, it is not an operator. Evaluate byte.
Int a = sizeof (int );
Here, we can find that the int type occupies 4 bytes (this may vary depending on the personal computer version, and my name is win7 64-bit ).
2. define. This is to give a new name for a thing, for example
# Define maxn100
In the future, when MAXN is used, it does not represent a string, but 100.
It can also be used to define functions.
# Define max (x, y) x> y? X: y
3. typedef
This is actually very similar to the previous one. This feature often appears when learning data structures,
For example, when you see
ElemType SqList ()
{
...
...
}
I think this ElemType is of any type.
Now you can find it at the beginning of the book.
Typedef int ElemType;
Here, we represent an alias for int, called ElemType.
This is done in the data structure to facilitate Bug tuning. There are so many basics.
I. What is struct first:
We all know the data types, such as int, char, float, and double. They are all data types that come with C language and other advanced languages.
Arrays are user-defined data types, and struct is also user-defined data types.
Example:
Int a [MAXN]; // defined as 10 before MAXN
Therefore, this array a can store 10 integer elements.
Char B [MAXN];
The B array can store 10 struct elements.
What if I want to store different types of elements in an array.
Here we have designed a good data type ----> struct.
Struct can be different data types or user-defined structural types that the system already has.
Example
Typedef struct
{
Int;
Char c;
Float c;
} TypeA;
This is a new data type, TypeA.
The specific call can be used as follows:
TypeA. a = 10;
TypeA. B = 'a ';
TypeA. c = 1.8;
...
...
...
You can also use this to define an array.
TypeA m [3];
At this time, each array element has three attributes.
M [0]. a = 20;
M [0]. B ='s ';
M [0]. c = 4.5;
...
...
...
The above is the use of struct. Do you understand it?
2. The next step is pointer
The pointer and struct are both data types, but the pointer variable contains the variable address.
It can locate the position of the variable in the memory, just like a pointer directing the direction, pointing out the position of a variable,
Therefore, it is called pointer type.
Define rules:
Int * a; // pointer to an integer variable
Char * B; // pointer to a struct variable
Float * c; // pointer to a floating point variable
TypeA * d; // pointer to the TypeA Type Variable
...
...
...
Here we can compare the direct definition of int ....
If a is a pointer variable and has already pointed to a variable B, the address where B is stored in.
* A is the content of variable B (X = * a; equivalent to x = B ;)
& B is the address of variable B.
A = & B; put the address of variable B in variable.
3. Mix struct and pointer
For example, the definition of a linked list:
Typedef struct Node
{
Int data; // The default type here is int. You can directly modify it if you need other types.
Struct Node * next; // pointer to a Node variable
} Node;
Binary Tree definition:
Tyedef struct BTNode
{
Int data;
Struct BTNode * lchild;
Struct BTNode * rchild;
} BTNode;
Yes
Tyedef struct BTNode
{
Int data;
Struct BTNode * lchild;
Struct BTNode * rchild;
} BTNode, * btnode;
* Btnode is added here.
When defining a node pointer p,
BTNode * p; equivalent to btnode p;
The usage here is different.
BTNode. data = 10;
(* BTNode)-> data = 10;
We can see that there is no difference,
If BTNode defines a common variable call as ".",
If the pointer type is->.
This is basically not easy to understand. You are also welcome to add this.
Do you understand?