当前位置:网站首页>Primary pointer part
Primary pointer part
2022-06-23 01:43:00 【'Dream_】
If you will It's better to start than to give up . Roman.
May we all have our own goals and are making unremitting efforts for them .
-----------------------------------------------------------------------
0. Add :
register ( Fast read ):ebp 、 esp ( The first two are addresses ) 、eax 、ebx 、ecx 、edx
****************************************
One 、 What is the pointer
One ) Two key points of pointer understanding
1. A pointer is the number of the smallest cell in memory , That's the address
2. The pointer in spoken English , Usually refers to pointer variables , Is a variable used to store the memory address
Two ) Add
1. The smallest unit in memory is One byte
2.
- 32 Bit virtual space :
CPU Generate 32 Bit address ( namely :32bit == 4byte)-- Transmission through the address line -- Navigate to memory space
- 64 Bit virtual space :
CPU Generate 64 Bit address ( namely :64bit == 8byte)-- Transmission through the address line -- Navigate to memory space
- notes : Each address line generates a high level when addressing ( high voltage ) And low level ( Low voltage ), namely 1 perhaps 0
- Every address (32 or 64 The root address line forms an address ) Identifies a byte , share (2^32 or 2^64) An address
3、 ... and ) Pointer to the variable
1. We can go through &( Take the address operator ) Take out the memory starting address of the variable , Put the address in a variable , This variable is the pointer variable
2. summary : Pointer to the variable , The variable used to hold the address .( Values stored in pointers are treated as addresses )
Four ) Summary
1. The pointer is the address , Pointer in colloquial language generally refers to pointer variable
2. The pointer is used to store the address , An address is an address that uniquely identifies an address space
3. Pointer size : stay 32 A platform --4 byte 64 A platform --8 byte
****************************************
Two 、 Pointers and pointer types
1. Pointer types To determine the The access permission of the pointer when it is dereferenced .( namely : The size of the operation )
Such as :① Integer pointer dereference access 4 byte
② Character pointer dereference access 1 byte
2. The pointer type determines the pointer forward or backward How far do you go
Such as :① int* + n --> skip n * sizeof (int) == n *4 Bytes
② char * + n --> skip n * sizeof (char) == n *1 Bytes
3. Add 1: sizeof(long) >= sizeof(int)
32 Bit environment : sizeof(long) == 4 byte
64 Bit environment : sizeof(long) == 8 byte
4. notes : sizeof(float) == 4 byte
5. Add :
- Whether it's a two-dimensional array or a one-dimensional array , You won't create an array when passing parameters
- When passing parameters to a one-dimensional array , The array size of formal parameters can be omitted
- When passing parameters to a two-dimensional array , In the array of formal parameters , Lines can be omitted , Columns cannot be omitted
- On declaration , If you want to omit rows from a two-dimensional array , Be sure to initialize
- Integer and floating point data , Different storage methods in memory
Such as :
float n = 3.14f ;
int * p = &n;
*p ; // ??? Unknown
****************************************
3、 ... and 、 Wild pointer
The position of the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to )
One ) The origin of wild pointer
1. Pointer variable not initialized
1) Add : If the local variable is not initialized, it will be automatically initialized to a random value
2) How to avoid :
- If the variable is known , Then take the address
- If unknown , Then assign NULL( The essence is 0),0 Is not allowed to be accessed , So the access pointer is conditional : Pointer variable is not equal to NULL
3) notes : Why? 0 Can't be visited ?
There are some spaces in memory that cannot be accessed by users , Just for the operating system ( kernel ) visit , This space includes 0
2. Pointer cross boundary access
#include <stdio.h>
int main()
{
int arr[10] = {0};
int *p = arr;
int i = 0;
for(i=0; i<=11; i++) // i The actual range of should be : 0-9
{
// When the pointer points to an array arr The scope of time ,p It's a wild pointer
*(p++) = i;
}
3. The space that the pointer points to is released
Such as :
int* test()
{
int a = 10;
printf("%d\n",a);
return &a;
}
int main()
{
int* p = test();
*p = 100;
return 0;
}notes : Local variables are destroyed after they exit the function , Space release , The variable pointed to by this address cannot be found for assignment
Two ) How to avoid wild pointer
1. Pointer initialization
2. Watch out for the pointer
3. The space that the pointer points to is released , Just in time NULL : Set it when you don't want to use it NULL
4. Avoid returning the address of a local variable
5. Check the effect before using the pointer : namely - Judge that the pointer variable is not equal to NULL
****************************************
Four 、 Pointer arithmetic
1. Add : As the array subscript increases , The address changes from low to high
2. Pointer operation refers to : The pointer can be used for size comparison
3. The pointer + - Integers : Such as *vp++; // +1 Skip an element
4. The pointer - The pointer
1) Premise : Both pointers must point to the same space , That is, the same array
2) result :( The pointer - The pointer ) The absolute value of The result is the number of elements between the pointer and the pointer
( The pointer - The pointer There are positive and negative )
5. example :my_strlen() -- Find array length ( Element number )
// notes : Three methods : Counter 、 recursive 、 The pointer - The pointer
// The pointer - The pointer : Only for character arrays
//my_strlen() -- Find array length ( Element number )
// The pointer - The pointer
#include<stdio.h>
int my_strlen(char* arr)
{
char* start = arr; // First element address
while (*arr)// seek \0-- At the end of
{
arr++; // Skip an element
}
return arr - start;
}
int main()
{
char arr[] = "abcdefghi";
int len = my_strlen(arr);
printf(" length :%d\n", len);
return 0;
}6. The relational operation of pointers
1) namely : Compare sizes between pointers
2) example
for(vp = &values[N_VALUES]; vp > &values[0];){*--vp = 0; // Reduce first , Reuse ( assignment )}Code simplification , This modifies the code as follows :for(vp = &values[N_VALUES-1]; vp >= &values[0];vp--){*vp = 0;}// In fact, most compilers can successfully complete the task , However, we should avoid writing like this , Because the standard does not guarantee that it is feasible .
- The standard stipulates :
****************************************
5、 ... and 、 Pointers and arrays
1. Arrays can be accessed through pointers
2. The array name is the first element address , But there are two exceptions :
- sizeof( Array name ), The array name represents the entire array , It calculates the size of the entire array
- & Array name , The array name represents the entire array , It takes out the address of the entire array
+1 Represents skipping the entire array , Such as :& Array name +1
- Add :
① Arrays can be accessed through pointers , Premise is : Arrays are stored continuously , It's the same space
② Arrays and pointers are not the same thing :
An array is a continuous space , Pointers are variables that store addresses
You can access the array through a pointer
****************************************
6、 ... and 、 The secondary pointer
1. The secondary pointer is The pointer of the pointer
2. give an example :
// The secondary pointer
#include<stdio.h>
int main()
{
int a = 0;
int* pa = &a;
int** ppa = &pa;
return 0;
}
// int * pa : * Express pa It's a pointer variable , int explain pa Pointing to int Data of type
// int* * ppa : *(ppa It was closest to ppa Of *) Express ppa It's a pointer variable , int* explain ppa The object is int* Data of type
// *ppa == pa ;
// *pa == a;
// **ppa == a;****************************************
7、 ... and 、 Pointer array
Pointer array It's for storage. 【 Pointer types 】 Of Array
1. Such as : int* arr[5]; // Pointer array , The type of element is int*
2. Simulate two-dimensional arrays : Pointer emulation
// Simulate two-dimensional arrays : Pointer emulation
#include<stdio.h>
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 2,3,4,5,6 };
int arr3[] = { 3,4,5,6,7 };
// The element types are the same
int** num[] = { &arr1,&arr2,&arr3 };// The array name is the address of the first element , Address is pointer
// The secondary pointer is the address of the address
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(num) / sizeof(num[0]);
int i = 0;
for (i = 0; i < size2; i++) // Decision line ( Think of each one-dimensional array as a row )
{
int j = 0;
for (j = 0; j < size1; j++) // Decision column ( The number of elements in each one-dimensional array is the column )
{
//printf("%d ", num[i][j]); // Mode one
printf("%d ", *(num[i]+j)); // Mode two
//num[i] Find the line -- That is to say i Address of the first element of a one-dimensional array , Add j-- That is to say i Line skip j Elements , After dereference, the element is obtained
}
printf("\n");
}
return 0;
}------------------------ All a person's anger comes from the pain of his incompetence .------------------------
边栏推荐
- 3. compilation and linking principle
- [ZOJ] P3228 Searching the String
- Extend your kubernetes API using the aggregation API
- Google benchmark user manual and examples
- 人民币的单位的大写
- [Luogu] P2887 Sunscreen G
- Byte order: big endian vs little endian
- 2D prefix and
- 1. introduction to MySQL database connection pool function technology points
- C language student achievement ranking system
猜你喜欢

Webdriver and selenium Usage Summary

Template specialization template <>
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

Philosopher's walk gym divide and conquer + fractal

office2016+visio2016

How are pub and sub connected in ros1?

Constexpr keyword

3D printing microstructure

Autumn move script B

5. explain function overloading
随机推荐
[hdu] P6964 I love counting
C. Diluc and Kaeya——Codeforces Round #724 (Div. 2)
[hdu] p2087 cut cloth strip
Population standard deviation and sample standard deviation
SQL programming task06 assignment - Autumn recruit secret script ABC
What aspects of language and database knowledge are needed to build a web Kanban!
C. Unstable String
Pat class A - 1013 battle over cities
4. functions and inline functions with default values for formal parameters
Webdriver and selenium Usage Summary
There are animation characters interacting with each other when the mouse slides in the web page
6. const usage, combination of first and second level pointers
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
时间复杂度
B tree and b+ tree
MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)
人民币的单位的大写
Autumn move script a
Vector 2 (friend and copy construction)
SYSTEMd summary