C (7), i75775c
C pointer advanced
This chapter introduces:
In the previous section 5th, we made a preliminary understanding of the C language pointer. As the soul of the C language,
The C pointer is certainly not that simple. In this section, we will further learn the pointer, such as the second-level pointer,
Pointer array, memory allocation, and const modifier pointer constants! Next, please follow the steps of the author
The mysterious side of the C pointer should be further parsed!
This section describes the road map:
Function and pointer: ① the pointer is used as the parameter of the function:
② Pointer to the function:
③ Pointer function:
④ Main functions with Parameters
Ps: run the code: compile and generate the exe file, and then go to the directory where the exe is located: input: File Name one two three
Output: one two three
Pointer array:
Two questions: how to save a string? What if more than one is saved?
First question:
① Char name [20] = "~~~ "② Char * name = "~~~ "// The former defines the length and cannot be assigned directly. Obviously, the latter is more flexible.
Second question: see Figure
Level 2 pointer:
Dynamic Memory Allocation:
Sample Code:
Dynamically allocate the space of 10 integer types. If the size is greater than this value, it is dynamically allocated. Otherwise, realloc is used to re-allocate the memory.
Ps: the code is correct. vc6 always reports a running error. If an error occurs, use the C-Free or Visual studio compiler to try it!
- <Span style = "font-family: Microsoft YaHei;" >#include <stdio. h>
- # Include <stdlib. h>
- # Define N 10
- Int main ()
- {
- Int * p = 0;
- Int I, num, * q = 0;
- P = (int *) malloc (N * sizeof (int )));
- If (p = 0)
- {
- Printf ("Memory Allocation Error! \ N ");
- Exit (0 );
- }
- Printf ("Enter the number of elements to store: \ n ");
- Scanf ("% d", & num );
- If (num <= N)
- {
- For (I = 0; I <N; I ++)
- Scanf ("% d", p + I );
- }
- Else
- {
- For (I = 0; I <num; I ++)
- Q = (int *) realloc (p, (N + N) * sizeof (int ));
- If (q = 0) exit (0 );
- For (I = 0; I <num; I ++)
- Scanf ("% d", q + I );
- P = q;
- }
- For (I = 0; I <num; I ++)
- Printf ("% 3d", * (p + I ));
- Printf ("\ n ");
- Free (p );
- Return 0;
- } </Span>
Use const to modify pointer Variables
We all know that using const to modify a variable of the basic data type can change the value stored in the variable from start to end.
Our program cannot be modified! However, modifying pointer variables is a little different:
For example:
Int a = 3, B = 4; const int * p = &;
If we add another code: p = & B;, an error will be reported.
If you add: a = 5; the Code does not have an error, * p = a = 5;
Analysis result:
When the const is used to modify the pointer, it indicates that the pointer is a constant pointer, and the value of the variable it points to can be modified;
However, it is wrong to modify the address pointed to by the pointer !!!
Many standard library functions in C add const before some pointer parameter types to protect parameters!
Ps: int * const p = & a; const int * const p = & a; is equivalent to the above!
Summary of this chapter:
① Functions and pointers:
1. pointer as the function parameter: The real parameter corresponding to the pointer parameter must be the variable address
2. pointer to the function: the first address of the function is called the pointer of the function. The function name can be used to locate and execute the function.
3. pointer function: the return value type is a pointer.
4. main Function with parameters: int main (int argc, char * argv []): the parameters are in sequence: the number of record parameters and the string array storing the parameter content
② Pointer Array
Comparison between two-dimensional array string storage and pointer array string Storage
③ Second-level pointer
Pointer to pointer variable
④ Dynamic memory allocation:
1. Memory Distribution chart
2. Use of four memory allocation functions provided by C: malloc (), calloc (), realloc (), and free ()
⑤ Use const to modify pointer Variables
The variable value pointing to the address that can be modified. What cannot be modified is the address pointed to by the pointer!
Reference: http://blog.csdn.net/coder_pig/article/details/37915063
Copyright Disclaimer: you are welcome to reprint it. I hope you can add the original article address while reprinting it. Thank you for your cooperation.