当前位置:网站首页>Standard C language learning summary 7
Standard C language learning summary 7
2022-07-28 07:07:00 【c7473168】
One , Problems needing attention when using pointers :
Null pointer : The value is NULL The pointer variable of is called null pointer ,
If the null pointer is dereferenced, a segment error will occur
NULL Generally as an error sign , When the return value of a function is a pointer type , have access to NULL
As the return result of function execution error
How to avoid segment errors caused by null pointers :
Make a judgment before using an unknown pointer
if(NULL == p)
1, When the parameter of a function is a pointer , The pointer passed to you by others may be a null pointer
2, When the return value obtained from the function is a pointer type , A null pointer may be returned
Be careful :NULL In most systems 0, Individually 1
Wild pointer :
Pointers to uncertain memory spaces are called wild pointers
Consequences of dereferencing the field pointer :
1, Everything is all right
2, Segment error
3, Dirty data
Wild pointers are more harmful than null pointers , Because it can't judge ,
And it may be a hidden mistake , Not exposed for a short time
All wild pointers are made by programmers themselves , How to avoid generating wild pointers
1, Be sure to initialize when defining pointer variables
int* p = NULL;
2, Function does not return stack memory ( Local variables in a function ) The address of
3, After the memory pointed to by the pointer is released , Pointer variables should be set to null in time NULL
Two , The operation of the pointer
Pointer variables store integers , Theoretically, all operators that can be used by integers can be used ,
But most operators are meaningless
The pointer + n The pointer + Pointer type width *num It's equivalent to moving forward n Elements
The pointer - n The pointer - Pointer type width *num It's equivalent to retreating n Elements
The pointer - The pointer ( The pointer - The pointer )/ Pointer type width Calculate how many pointer elements are directly separated between two pointers
3、 ... and , The pointer and const
When we use pointers as function parameters to improve the efficiency of parameter transfer , The efficiency of parameter transmission is improved
But there is a risk that variables will be shared and modified , have access to const Protect the memory pointed to by the pointer
const int* p; Protect the memory pointed to by the pointer from being modified
int const *p; ditto
int* const p; Protect pointer variables from modification
const int* const p; Neither the pointer variable nor the memory pointed to by the pointer can be modified
int const * const p; ditto
Four , Pointer arrays and array pointers
Pointer array :
The array composed of pointer variables is called pointer array , Its members are pointer variables of the same type
type * arr[ length ];
int* arr[10];
Array pointer :
Is a pointer to an array
type (*arrp)[ length ];
int (*arrp)[10];
5、 ... and , Array name and pointer :
Array names are special pointers
The array name is a constant , You can't change its value , The array name does not have its own storage space between it and the first address of the array
Is a mapping relationship
Array name == & Array name
Pointer variables have their own storage space , It and Institute Directional relation
When the pointer variable points to the first address of the array , Pointers can be used as array names , The array name can also be used as a pointer
Array name [i] == *( Array name +i)
*(p+i) == p[i]
Be careful : When an array is used as a parameter of a function, it becomes a pointer , So the length is lost
6、 ... and , The secondary pointer
The secondary pointer is the pointer to the pointer , What is stored inside is the address of the pointer variable
Definition : Type name ** Variable name _pp
assignment : Variable name _pp = & Pointer to the variable ;
Quoting :
* Variable name _pp <==> Pointer to the variable
** Variable name _pp <==> * Pointer to the variable <==> data
Be careful : When you need to share pointer variables between functions , Pass the address of the pointer ( The secondary pointer )
7、 ... and , A function pointer
The function name is an address , The function name represents the entry position of the function in the code segment
A function pointer is a pointer to a function , It stores the entry location address of the function in the code segment
return type (*p)( type 1, type 2,...);
int (*funcp)(int,double); //funcp Specifically point to func Function pointer of
You can use function pointers , Pass a function as an argument to another function , This method is called function callback mode
边栏推荐
- [learning notes] coding ability
- DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
- 360 compatibility issues
- Codesensor: convert the code into AST and then into text vector
- kali下安装nessus
- Group management and permission management
- MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算
- Repair the faulty sector
- shell---条件语句练习
- The.Joernindex database has no content after Joern runs
猜你喜欢

PXE无人值守安装管理

MOOC Weng Kai C language fourth week: further judgment and circulation: 3. Multiple branches 4. Examples of circulation 5. Common errors in judgment and circulation

MOOC Weng Kai C language fourth week: further judgment and circulation: 1. Logical types and operations 2. Judgment of cascading and nesting

Principle and configuration of NAT and pat

How to describe a bug and the definition and life cycle of bug level

Method of designing test cases

Joern's code uses -devign

About gcc:multiple definition of

Custom components -- slots

MOOC翁恺 C语言 第三周:判断与循环:2.循环
随机推荐
Custom component -- data listener
Results fill in the blanks for beer and drinks
Shell script - regular expression
Forward and backward slash notes
easypoi导出表格带echars图表
Pictures are adaptive to the screen
Es6--- > arrow function, class, modularization
Small turtle C (Chapter 5 loop control structure program 567) break and continue statements
Uni app double click event simulation
win下安装nessus
Implementation method of converting ast into word vector before converting word vector
Reptile learning summary
The.Joernindex database has no content after Joern runs
爬虫学习总结
Applets: WSX scripts
Codesensor: convert the code into AST and then into text vector
Custom components -- slots
Network - network layer
YUM仓库的搭建
Custom component -- communication between parent and child components