C language basics 10, c language 10
Stack interval: all variables declared in the function are stored in the stack interval. For example, if the int char array struct pointer is applied, the system automatically recycles the variable. The retrieved time is after the scope ends, the principle is "advanced and later ".
Int a = 10;
Const int m = 10;
Printf ("% p \ n", & m );
Printf ("% p \ n", & );
Heap interval: when the five intervals occupy a large proportion, You need to manually apply for and release them. Follow the principle of "first-in-first-out". The heap interval space is unordered, there is no name, so we need a stack pointer to point to the value of the heap memory.
Int * B = malloc (sizeof (int) * 2 );
Printf ("% p \ n", B );
Free (B );
Static zone: variables defined in the external body of the function. Stores global variables or static variables. The lifecycle is long and is generally released until the end of the program. Variables modified using static are initialized only once. The default value is 0.
Static int c = 23;
Printf ("% p \ n", & c );
Void test3 (){
Static int a = 10; the initial value is assigned when the function is called for the first time, and the statement that calls the function later will not be allowed.
A ++;
Printf ("% d \ n", a); // output 10, 11, 12
}
Test3 ();
Test3 ();
Test3 ();
Constant Area: constants we encounter are put here, such as "myname", 'C', 87, 87.4
Printf ("% p \ n", & "hello world ");
Constant zone variables cannot be modified:
Char * str = "iphone"; // The character Pointer Points to a string in the constant area.
Char str1 [] = "iphone"; // copy the constant zone string to the space in the stack zone where the pointer name is str1.
Str1 [0] = 'I'; // it can be modified. No error is reported during code compilation, but an error is reported during compilation.
Str [0] = 'I'; // an error is returned.
So we generally write like this: const char * str = ".... "The advantage of writing this is that in a large project, when we modify the value of the constant zone variable, an error is reported, which can be handled immediately, rather than an error after compilation and running, find the error source from tens of thousands of lines of code.
Code area: Compile the source code into a binary file, which is generally a function.
Void func ();
Void func (){
Printf (...)'
}
Printf ("% p \ n", func); // input the function name.
Function for allocating memory space:
When malloc is finally released, it does not actually "empty", but indicates that the space has been used, and the stored values may still exist.
Char * str = malloc (sizeof (char) * 30 );
Printf ("% p \ n", str );
Free (str );
Free (str); // release cannot be repeated and will crash, because there is no space for the corresponding pointer name to be released for you. For security reasons, we often add it after free.
Str = NULL;
Calloc opens the memory space and clears the value. If the data is large, the efficiency is very low.
Char * str1 = calloc (sizeof (char), 30 );
Printf ("% p \ n", str1 );
Free (str1 );
Str1 = NULL;
Realloc reopens space based on the given memory address, which is generally a larger space.
Int * num1 = malloc (sizeof (int) * 10 );
Realloc (num1, 40); // expands the space of 10 bytes to 40 bytes.
Free (num1 );
Num1 = NULL;
Memset assigns an initial value to the newly opened space.
Char * str3 = malloc (sizeof (char) * 20 );
Memset (str3, 'M', 20 );
Free (str3 );
Str3 = NULL;
Memcpy copy
Char * str4 = malloc (sizeof (char) * 20 );
Memcpy (str4, "luoshuailuo", 6); // copy the string luoshuailuo to str4. only 6 characters are copied.
Free (str4 );
Str4 = NULL;
Memcmp comparison q
Char * name = malloc (sizeof (char) * 100 );
Char * name1 = malloc (sizeof (char) * 100 );
Strcpy (name, "LUOSHUAI ");
Strcpy ("name1", "luoshuai ");
Memcmp (name, name1, 100 );
Free (name );
Free (name1 );
Name = NULL;
Name1 = NULL;
There is a string containing characters and numbers. numbers must be extracted and dynamically allocated for storage.
Int * str = "iphone6and8Plus23cc84 ";
Int count = 0;
For (int I = 0; I <strlen (str); I ++ ){
If (* (str + I)> = '0' & * (str + I) <= '9 '){
Count ++;
}
}
Int * num = malloc (sizeof (int) * count );
Int count = 0;
For (int I = 0; I <strlen (str); I ++ ){
If (* (str + I)> = '0' & * (str + I) <= '9 '){
* (Num + count) = * (str + I)-48; // reduce the number of characters to be converted to a number by 48
Count ++;
}
}
For (int I = 0; I <count; I ++ ){
Printf ("% d \ n", * (num + I ));
}
Free (num );
Num = NULL;
Self-learning accumulation:
Strncpy (destion, source, num); this function is used to copy the num-length string of "source" to "destion ",
If the length of num <source, copy the num strings before source to source without adding \ 0.
If num = source length, all characters of the source are copied to the source, and \ 0 is not added.
If the length of num> source, all the characters of source are copied to source, followed by \ 0, and each character is added as many as one.