C language-pointer, C language pointer
No pointer, not free
----------------------------
-- 1 -- why use pointer1.1 Basic concepts of pointers1.2 advantages of using pointers1.3 storage of Variables-- 2 -- pointer variable 2.1 pointer variable2.2 define a pointer variable 2.3 pointer variable Initialization Method2.4 use * to get the content of the corresponding storage area of the pointer -- 3 -- storage details3.1 variables and pointer Variables-- 4 -- common application scenarios4.1 common application scenarios of pointers-- 5 -- Introduction to multilevel pointers5.1 second-level pointer5.2 introduction to multilevel pointers
----------------------------
[Written at the beginning :]
"Pointer in life:
That's right. It's a house number.
Pointers are one of the most important aspects in C language.
Why use pointers?
If you want to find a document and give you a thick encyclopedia, you only need 3,001st pages of content.
In this case, the 3001 page is a pointer. If you don't have this pointer... okay, I'll cry next to it...
』
-- 1 -- Why use the basic concept of pointer 1.1?
The memory unit number is also called the address. You can find the memory unit according to the memory unit number or address.
Therefore, we usually call this address a pointer.
1.2 advantages of using pointers
1) provides flexible means for the function to modify call Variables
2) Let the function have multiple return values
3) Improve Program Efficiency
When transferring data, if the data block is large (such as a data buffer or a large structure), you can use the pointer transmission address instead of the actual data, which improves the transmission speed, saves a lot of memory
4) provides support for dynamic data structures (such as binary and linked lists)
1.3 storage of Variables
There are two ways to access variables: direct access and indirect access.
1) Direct Access: assign values and values of Variables
2) indirect access: Indirect operations are completed through pointers (addresses ).
One of the biggest functions of pointers is to operate (ACCESS) variables and arrays through addresses.
-- 2 -- pointer variable 2.1 pointer variable
In C, a variable is allowed to store pointers. This variable is called a pointer variable.
The pointer variable stores an address and a variable.
2.2 define a pointer variable
The pointer variable definition includes three parts:
1) pointer type description, that is, defining a variable as a pointer variable
2) pointer variable name
3) variable value (address)
General format:
Type specifier * variable name = address;
Int * p; // defines a pointer variable. The variable name is p int, indicating that p points to an int type variable space char * p1; // The variable name is p1 char, indicating that p1 points to a char-type variable space
Where
Type specifier indicates the Data Type of the variable pointed to by the pointer.
* Indicates that this is a pointer variable.
The variable name is the defined pointer variable name.
Note:
1) when defining a pointer, * indicates that the defined variable is a pointer variable (if not *, it is no different from a common variable)
2) can pointer variables be used to store arrays or characters? --> NO (the address is stored)
3) A pointer of the same type can only point to a variable of the same type, but cannot point to other types of variables.
4) pointer variables are ultimately a variable, which also has global and local points.
2.3 pointer variable Initialization Method
Pointer variables should be initialized with addresses
Two initialization methods:
Initialize the definition at the same time, first define and then initialize
1) Definition initialization at the same time
// Fully initialize int a = 3; int B = 4; int * P = &; // use the address of a to initialize the pointer Variable p (also known as p pointing to a) int * p1 = & a, * p2 = & a; // p1, p2 points to.
2) Define and initialize
// Partially initialize int * p3 = & B, * p4; // defines two pointer variables p3 and p4 p4 not initialized p4 = & B;
2.4 use * to get the content of the corresponding storage area of the pointer
Like normal variables, pointer variables must be defined and assigned with specific values before use.
Pointer variables that are not assigned values cannot be used. Otherwise, system confusion or even a crash (wild pointer) may occur ). The pointer variable value can only be assigned to the address, but cannot be assigned to any other data. Otherwise, an error occurs.
In the C language, the variable address is allocated by the compilation system and is completely transparent to users. users do not know the specific address of the variable in advance (but can know the region location ).
Two related operators:
&: Obtains the address operator;
*: Pointer operator (or "indirect access" operator );
The C language provides the address operator "&" to indicate the address of a variable. The general form is: & variable name;
Get the content of the bucket to which the pointer variable points: * pointer variable name
* The operator is the opposite of the & operator. & The operator receives a data and tells you where the data is stored. * The operator receives an address and then tells you what data is saved in the address.
Because pointers are sometimes referred to as references, * operators can also describe pair pointers for unreferencing.
Int a = 3; int B = 4; int * p1 = & a; int * p2 = & B; // * pointer variable: get the content (value) of the memory space pointed to by the pointer variable and assign it to value int value = * p1; // 3 * p1 = 100; // change the space value = * p2; // 4 printf ("* p1 = % d \ n", * p1 ); /// 100 printf ("value = % d \ n", value); // 4
Thoughts
Define the following pointer
float *pointer_3; char *pointer_4; int a,b; int *pointer_1 = &a, *pointer_2;
Determine whether the following operations are valid
*pointer_1 = &a; pointer_3 = 2000; pointer_3 = &a; pointer_1 = &a; pointer_2 = pointer_4;
Analysis:
* Pointer_1 = & a; // error; * pointer_1 is a solution reference and a value, not the address pointer_3 = 2000; // error; the pointer_3 pointer is not initialized, the value must be assigned to an address pointer_3 = & a; // error; a float address should be assigned to pointer_3 pointer_1 = & a; // valid pointer_2 = pointer_4; // error; Different Types
-- 3 -- storage details 3.1: storage details of variables and pointer Variables
Every time a variable is declared, the computer creates a space for it somewhere in the memory. If a variable is declared in a function (such as the main () function), the computer saves it in a memory segment called a Stack; if you declare a variable outside of a function, the computer will save it in the global volume segment (Globals) of the memory ).
Int y = 1; // the address in the global segment: 1000 000int main (int argc, const char * argv []) {@ autoreleasepool {int x = 4; // stack-specific address: 4100 000} return 0 ;}
The following variables are available:
Int I = 200;
Int * p = & I;
At this time, p points to the first address of variable I & I
-- 4 -- common application scenarios 4.1 pointer common application scenarios
This is a common scenario. When passing parameters between functions, you must use the called function to change the values of the variables in the main function.
For example, a function that exchanges two variables. If no pointer is used, a method such as a temporary variable is usually used.
Int main (int argc, const char * argv []) {@ autoreleasepool {int x = 3, y = 4, temp; // The value temp = x; x = y; y = temp; printf ("x = % d, y = % d \ n", x, y);} return 0 ;}
However, the above statement is not good, and it is necessary to encapsulate it (forgive me for being inaccurate here, "encapsulation" is a feature of object-oriented language) into a custom method.
So if it is encapsulated (used to call it like this ...) after a custom method is passed, if the real parameter is a variable, the value of the variable in the main function will not change, because the form parameter actually opens up a new memory space, the values of real parameters cannot be modified in user-defined functions:
Int main (int argc, const char * argv []) {@ autoreleasepool {void swap (int x, int y); // function declaration int x = 3, y = 4; swap (x, y); // The value of printf ("x = % d, y = % d \ n", x, y) in the real parameter space cannot be modified as a real parameter ); // The Test Method of x = 3, y = 4} return 0;}/*** is not changed, swap variable value ** @ param x variable 1 * @ param y variable 2 */void swap (int x, int y) {// The value of the temporary variable is int temp = x; x = y; y = temp ;}
Therefore, to implement the above set scenario, you can only use the pointer address for transmission:
Int main (int argc, const char * argv []) {@ autoreleasepool {void swap (int * x, int * y); // function declaration int x = 3, y = 4; swap (& x, & y); // you can use the address as the form parameter to exchange the values of x and y in printf ("x = % d, y = % d \ n ", x, y); // x = 4, y = 3} return 0 ;} /*** exchange the values of two variables ** @ param x address 1 * @ param y address 2 */void swap (int * x, int * y) {// The value of the temporary variable exchange two variables: int temp = * x; * x = * y; * y = temp ;}
-- 5 -- Multi-level pointer introduction 5.1 second-level pointer
If a pointer variable stores the address of another pointer variable, it is called a pointer variable pointing to the pointer, also known as a "second-level pointer"
Access variables through pointers is called indirect access. Because pointer variables direct to variables, they are called "first-level Pointers ". If you access the variable by pointing to the pointer variable, it constitutes a "second-level pointer"
Int a = 5; int * p = & a; printf ("% p \ n", & a); // address of a printf ("% p \ n ", p); // The address printf ("----- \ n") of a is stored in Pointer p; // ***** Level 2 pointer int ** p1 = & p; // a pointer printf ("p = % p \ n", & p) is stored in p1 ); // pointer p address printf ("p1 --> % p \ n", p1); // address stored in p1 printf ("---------------------- \ n "); printf ("* p = % d \ n", * p); // printf ("* p1 = % p \ n ", * p1); // the pointer address stored in the address pointed to by p1 printf ("** p1 = % d \ n", ** p1 ); // unreference the pointer stored in p1
5.2 introduction to multilevel pointers
Similarly, how many * pointers are defined?
[Written at the end :]
"Everyone should have a dream when they are young 』