当前位置:网站首页>C language first level pointer
C language first level pointer
2022-07-29 05:27:00 【Tortilla 85】
First level pointer
Learning goals :
Master the first level pointer knowledge
- Master the first level pointer knowledge
Learning content :
1. Pointer basis :
1.1 Definition of pointer
Pointer definition : data type * Variable name
Such as :int * p
int Is the data type of the pointer
p Is the variable name of the pointer
about * The function of is to reference the variable value pointed to by the pointer , Reference refers to the address of the variable
int a = 10;
int* p = &a ;
& Here is the address character
int * p = &a; To make p Pointer to a The address of , Pictured above .
after p The address of 0x002 What is stored in is a The address of 0x001.
1.2 The size of the pointer
Pointer size is determined by CPU The addressing bits of the operation mode determine
One byte =8 position
The processor is in 32 Bit operation mode , Logical addressing bits 32, The pointer is 32 position , namely 4 Bytes
The processor is in 64 Bit operation mode , Logical addressing bits 64, The pointer is 64 position , namely 8 Bytes
When running a program in the compiler , The pointer size varies with the running environment
2. Pointer dereference function
Dereference is to return the corresponding object in the memory address
Quoting : * Variable of pointer type
Such as 1.1 Definition of pointer In the code p The address is stored a The address of ,p Is equal to a The address of ,* p It is equal to returning a The value in the address .
modify a or * p The value of is modified a Address Value inside .
Example :
void add(int *val)
{
(*val)++;
}
int main()
{
int a = 1;
add(&a);
printf("%d \n", a);
return 0;
}
Running results :
The function in the code above add() Take the integer pointer as a formal parameter ,val Is the variable name of the pointer , * val Is the value of the transmitted address .
adopt main Function will a The address of the pass in function add() in , stay add() Function , Shape parameter val The address for a The address of , * val by a Value . Yes a Add one to the data in the address (*val)++, That is to say a++, Final a = 2.
3. Windows data storage
X86(32 position ) Operation in environment :
stay windows in , Data is stored in small end mode ( The low order data is stored in the low order address )
The stack area : When using compilers , The compiler will automatically allocate and free memory , To assign local variables to the running program , Function parameter , Return the data , Return address, etc .
The address size is 32 position ,4 byte .
from 0x0000 0000 To 0xffff ffff
int main()
{
int a = 1;
int b = 2;
return 0;
}
The main function main() Before running , Is already a variable a,b Allocated memory space .
Stack memory partition By high address To The order of low addresses .
1 The space is allocated to a, Deposit a Value 1.
2 Assigned to b, Deposit b Value 2.
The storage of arrays in the stack :
First, according to the size of the array, open up enough memory space from high address to low address , Then in the opened memory space, follow the small end mode , Store the data in the array from low address to high address .
Array arr[4] ={1,2,3,4}; The process is as follows
4. The operation of the pointer
You can access the addresses of different positions by adding and subtracting pointer variables , To get the effect you want .
4.1 give an example :
4.1.1 Access array
int main()
{
int arr[4] = {
1,2,3,4};
int* p = &arr[0];
for(int i = 0;i<4;i++)
{
printf("%-3d",*(p++));
}
return 0;
}
Running results :
Through the address of the pointer after ++ To get arr Address in array , It is used to output the value in the address by dereference .
4.1.2 Before the m Put the number behind
function Reverse1: Yes begin To end Data in reverse order With a pointer
void Reverse1(int* arr, int begin, int end)// Yes begin To end Data in reverse order With a pointer
{
assert(arr != NULL && begin <= end);
int temp = 0;
int* brr = arr;
int len = (end - begin + 1) / 2;
for (int i = 0; i < len; i++, begin++, end--)
{
temp = *(arr + begin);
*(arr + begin) = *(brr + end);
*(brr + end) = temp;
}
}
function Adjust: Before the m Put the number behind
void Adjust(int* arr, int len, int m) // Before the m Put the number behind
{
assert(arr != NULL);
Reverse1(arr, 0, len-1);// Yes arr The whole is in reverse order
Reverse1(arr, 0, len - 1 - m);// To the front len-m Elements in reverse order
Reverse1(arr, len - m, len - 1);// Right back m Elements in reverse order
}
int main()
{
int arr[] = {
1,2,3,4,5,6,7 };
int len = sizeof(arr) / sizeof(arr[0]);
Adjust(arr, len, 4);
for (int i = 0; i < len; i++)
{
printf("%-2d", arr[i]);
}
return 0;
}
Running results :
边栏推荐
猜你喜欢
一维数组练习
OCCT学习002-----环境搭建
Cmu15-213 malloc lab experiment record
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
Best practices of JD cloud Distributed Link Tracking in financial scenarios
Occt learning 003 - MFC single document project
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
01-01-osg GL3 环境搭建
阿里云架构师细说游戏行业九大趋势
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
随机推荐
QT learning: qdropevent drag event
京东云分布式链路追踪在金融场景的最佳实践
直播预告:京东云DevOps与JFrog制品库的融合
【C语言系列】— 不创造第三个变量,实现两个数的交换
阿里云张新涛:异构计算为数字经济提供澎湃动力
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
Solution: find the position of the first and last element in a sorted array (personal notes)
【C语言系列】— 把同学弄糊涂的 “常量” 与 “变量”
167. Sum of two numbers II - enter an ordered array
阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
·来一篇编程之路的自我介绍吧·
C语言 一级指针
研发效能生态完整图谱&DevOps工具选型必看
Differences between texture2d and texture2dproj under webgl1.0
三次握手四次挥手针对面试总结
【C语言系列】— 打印100~200之间的素数
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
GPIO的输入输出详解
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
Bubble sort c language