Abstract:
If beginners are not familiar with the syntax of C language, they often feel confused when reading the C language textbooks of Yan Weimin edition. They are dizzy in the details of the source code. If they are too entangled in the details, they blur the essence of the algorithm. This is what I did during my first reading. So when I re-read the data structure teaching material for the second time, I spent some time sorting out the grammar that is often used in the book and is easy to ignore when I learned the C language. I will share it with you. I read some articles on my blog and wrote well, but some of them are hard-reading. I try to understand them from the perspective of beginners, try to use simplified examples related to the teaching material code. In addition, I will share some links to articles that I think are good about as an extension. As you learn more, this blog post will be updated occasionally
The content includes:
(1) usage and significance of typedef.
(2). Parameters of the type referenced in the function (actually in C ++, but frequently used in the teaching material ).
(3) features of the struct pointer.
(4) create and use dynamic arrays.
(5). pointer array.
(6) Memory Allocation Management is extremely dynamic in the struct pointer array.
Body:
(1) usage and significance of typedef
Typedef is used to redefine the type name. You can rename the existing type name and the Organization Name Defined by yourself into a name that is pleasing to the eye, you can use both the original name and the new name you have defined when referencing. For example:
Typedef int status; // in the source code of the data structure of Yan Weimin, Int type is defined as status. status is often used to define the function type, indicating the status returned after the function is run.
Typedef struct LNode {
ElemType data;
Struct LNode * next;
} * Link, * Position;
/* When using this struct, for example, when initializing a struct of this type, you can either: struct LNode * node1;
It can also be like this: Link node1; or Position node1; in addition, when defining the struct, the original name of the red LNode location can be omitted.
There are many advantages of using typedef. The main functions of using typedef in teaching materials are as follows,
1 is to make the code easy to remember and improve code readability;
2 is to facilitate the reference of struct (compare the value assignment methods 1 and 2, you can find that the next struct keyword can be saved =|| ). If you are interested, read the instructions on using C language typedef in Baidu Library http://wenku.baidu.com/view/cd7a472d2af90242a895e570.html. */
(2) parameters of the reference type in the function
Let's look at a piece of code:
Void fun1 (int x) {x = 73; printf ("% d \ t", x);} // common type parameter
Void fun2 (int & x) {x = 73; printf ("% d \ t", x);} // reference type parameter
Void main
{
Int a = 87;
Int B = 87;
Fun1 ();
Fun2 (B );
Printf ("% d \ t", );
Printf ("% d \ t", B );
}
The output is 73 73 87 73, so. Obviously, when a parameter of the reference type is used, the address of the variable is passed as the form parameter. When the function changes the form parameter, the value of the original variable also changes, while the value of the original variable does not change even if the parameter size is changed in the function. This is why the value of variable can be returned for type parameters in the textbook.
(3) features of struct pointers
If the pointer feature is still dizzy, first read the original C language textbooks to see the pointer introduction, and find out the basic concepts. Next, let's talk about the features of struct pointers.
Char * text;/* in C, the string is processed as an array of character type. For string pointers on the left, text points to the first element address of a struct array, which is equivalent to text [] */
Text = "stay foolish, stayhungry."/* so you can directly assign a string to the text pointer, which is equivalent to text [] = "string"; */
Text [I] = "v";/* You can also assign a character to the I-th element of the array */
When getting confused, remember that the attention pointer is an array, an array, and an array! OK.
{4) create and use dynamic arrays
I remember that when I learned the C language, the concept of dynamic array seems to be a few simple words, and I did not know it, because I didn't know how to use it. I learned a little bit about electronic development before I found it very useful. Whether it is single-chip microcomputer development or other advanced embedded development, the memory size of the chip is basically very limited, you must be careful in order to avoid a crash due to insufficient memory. When we first started c, we built an array at will, for example, int numchain [MAXNUM]. In this way, after the call is created, the values are assigned, modified, and no problem occurs. The program ran happily, however, sometimes creating an array is only used in a certain part of the program. After the directly created array is used up, the memory space of the array is still occupied in the hardware physical memory, that is, when the memory is occupied, it is not allowed to put other data or operations. If a program is large and there are many dead arrays in it, the program runs and finds that the hardware memory is insufficient, it crashes, and this bug is hidden and cannot be checked. So the best habit is to develop the habit of using dynamic arrays.
So how to use dynamic arrays? View code segment:
# Include <stdlib. h> // functions such as malloc free are hidden in the header of this library. You need to include them first.
Void main
{
Char * text; // create a struct pointer, that is, a struct array.
Text = (char *) malloc (10 * sizeof (char ));
/* Take the malloc function with the highest frequency as an example. We can dynamically create a memory space for the text pointer, which is in a system space called heap, because the text pointer is in bytes type, and the return value of malloc itself is a hexadecimal address, you should convert it with some red code. How much space is malloc, that is, multiply 10 by the blue part of the code, that is, the size of 10 characters. */
Text = "I'm happy.";
/* Note that the length of the value assignment string should not exceed 10 at this time. Even if the value exceeds 10, the system can compile and execute the value, but more than 10 strings are randomly allocated to a memory area of the system, when free () is used to release the memory, the memory occupied by the strings that exceed 10 is not released completely and becomes the residual memory garbage. */
Free (text); // release the memory. Using this function, the dynamic memory allocation mechanism makes sense.
}
So, in summary, a dynamic array is actually to use the malloc function to allocate storage space to the array. When the array is used up, use free () to release it. So easy, isn' t it?
In addition to malloc, there are also calloc and realloc functions in C. For more information, click here:
Http://blog.csdn.net/bigloomy/article/details/6615012
(5) pointer Array
To put it simply, a pointer array refers to an array in which each element is a pointer (that is, each element is an address ).
For example, char * text [5];
This line of code specifies a text pointer array of the struct type. The text contains five elements: text [0] ~ Text [4], each element is a pointer of the struct type. As we have already said above, every pointer of the struct type can be considered as a struct type array, so this pointer ArrayIt is actually a two-dimensional array text [5] [], Text [I] can be used to assign a string, that is, the pointer array contains five strings, text [0] ~ Text [4:
Text [0] = "Hello ";
Text [1] = "world ";
Text [2] = ",";
Text [3] = "hello ";
Text [4] = "073 ";
Very easy, isn't it? As a result, the following information appears in the teaching material:
Typedef struct {
Char * item [];
Int last;
} WordListType;
It is not a problem to analyze it carefully. For example, you can perform the following operations on the above struct:
WordListType wdlist; // the middle position of the textbook P88
P = * (wdlist. item + I); // isn't this line of code equivalent to p = * (wdlist. item [I? Assign the string in the address pointed to by wdlist. item [I] to p. p is a character pointer, char * p;
(6) Memory Allocation and Management of balanced pointer Arrays
Well, this is actually a combination of the preceding (3) (4) (5) points. Suppose we want to establish a data structure that reflects the text section. There are several rows in a section, each row is a string, and what data structure should we use, what should I do when I initialize, modify, and print the data structure? Let's combine the above content and write the code segment as a summary:
# Include <stdlib. h>
# Include <iostream. h> // the input/output stream of c ++ is easier than the standard input/output of c. If the debugging effect is the same on pc, <stdio. h> is discarded.
Typedefstruct {
Char * text [5]; // you can set a maximum of five lines for a paragraph.
Int line; // used to identify the current number of rows.
} Paragraph;
Paragraphpara; // global variable para1
Void main ()
{
For (para. line = 0; para. line <5; para. line ++) // allocate dynamic memory space for paragraphs.
{
Para. text [para. line] = (char *) malloc (80 * sizeof (char); // assume that each line has a maximum of 80 characters.
}
For (para. line = 0; para. line <5; para. line ++) // enter the strings of each line and press enter to end the input.
{Cout <"Enter the" <para. line + 1 <"line string" <endl;
Cin. getline (para. text [para. line], 80 * sizeof (char); // This is a slide output function using c ++, which can implement the entire input line, with the carriage return as the identifier.
}
For (para. line = 0; para. line <5; para. line ++) // print each row.
Cout <para. text [para. line] <endl;
For (para. line = 0; para. line <5; para. line ++) // release the memory space of each row
Free (para. text [para. line]);
}