当前位置:网站首页>First acquaintance with C language - first acquaintance with pointer
First acquaintance with C language - first acquaintance with pointer
2022-07-27 05:37:00 【yin_ Yin】
List of articles
Recognize the pointer , First of all, we need to know what memory is .
1. Memory
Memory is a very important memory on a computer , All programs in a computer run in memory .
So in order to use memory efficiently , Just put Memory is divided into small memory units , The size of each memory unit is 1 Bytes .
In order to effectively access every unit of memory , Just give it to Memory units are numbered , These numbers are called the address of the memory unit .
How did these numbers come into being ?
stay 32/64 A platform , There is 32/64 Root address line , These address lines are physical lines , After the power is on , Generate electrical signals ( The positive charge is 1, Negatively charged 0), Then the electrical signal is converted into digital information , namely 32/ or 64 Bitwise by 0,1 A binary sequence of components , The binary sequence corresponding to each memory unit is its number .
We need to know , Every time we define a variable , You need memory to allocate a suitable space for this variable , For example, integer. int Distribute 4 Bytes ,char Distribute 1 Bytes ,double Distribute 8 Bytes .
Variables are created in memory ( To allocate space in memory ), Each memory cell has an address , therefore Variables also have addresses .
Take out the variable address as follows :
#include <stdio.h>
int main()
{
int num = 10;
#
printf("%p\n", &num);
return 0;
}


2. Pointer to the variable
Since the address of the variable can use the address operator (&) Take out , Can you put one The address of the variable is stored ?
Certainly. !!!
stay C Language , There is a special variable for storing addresses , be called Pointer to the variable .
Definition method of pointer variable :
type * Pointer variable name ;(* Indicates that the variable is a pointer variable )
Let's demonstrate :
int num = 10;
int *p;//p For an integer pointer variable
p = #

In this way, the address of an integer variable is placed in an integer pointer variable .
Now we know how to save the address of the variable , So can we Use our stored address to find this variable Well ?
Certainly. , It's like you have the address of a friend of yours , You can find his home through this address .
If you want to find this variable through the pointer , Still need to use This operator , Here it is called Dereference operator *.
for instance :
#include <stdio.h>
int main()
{
int num = 10;
int *p = #
*p = 20;
printf("%d\n", num);
return 0;
}
Look at this code , Print out num What are the results ?

Take the example of a reshaped pointer , It can be extended to other types , Such as :
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;
*pc = 'q';
printf("%c\n", ch);
return 0;
}

3. The size of the pointer variable
Think about a problem , The size of the integer variable is 4 Bytes ,char type 1 Bytes ,double8 Bytes , that What is the size of the pointer variable ? Is the size of different types of pointer variables different ?
Let's test it :
#include <stdio.h>
int main()
{
printf("%d\n", sizeof(char *));
printf("%d\n", sizeof(short *));
printf("%d\n", sizeof(int *));
printf("%d\n", sizeof(double *));
return 0;
}
What is the result of the operation ?
Why are different types of pointer variables the same size ? And why 4 Bytes? ?
as a result of :
The pointer is used to store the address , So the size of the pointer variable depends on the size of the address , On the same platform, the size of the address is fixed .
32 The address under the bit platform is 32 individual bit position ( namely 4 Bytes )
64 The address under the bit platform is 64 individual bit position ( namely 8 Bytes )
stay 32 A platform , The address of the memory unit is determined by 32 individual 1,0 The number that makes up the binary sequence , That's it 32 A bit , namely 4 Bytes .
Empathy , stay 64 A platform ,64 individual 0,1 The binary sequence of composition constitutes the number , That's it 64 A bit , namely 8 Bytes .
So let's verify that :
stay 32 A platform :
4 Bytes
64 A platform :
8 Bytes
therefore , We come to the conclusion that Conclusion :
The size of pointer variables is fixed on the same platform :
The size of the pointer is 32 Bit platform is 4 Bytes ,64 Bit platform is 8 Bytes .
The above is a preliminary understanding of pointer .
边栏推荐
- JS中apply、call、bind的区别
- [codeworks round 801 div2 D tree queries] tree greedy conclusion
- C language string introduction and related operation functions
- 后台用户管理展示添加功能实现
- [codeforces round 800 D question fake plastic trees] greedy on the tree
- Hi3516DV300环境搭建
- md5 密码加密
- p7 day1 初识Flask框架
- Simplify the mybits framework of JDBC
- 我的第一篇博客
猜你喜欢
随机推荐
Xiaomi mall project_ register
Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)
Cenos7更新MariaDB
通用视图,DRF视图回顾
md5 密码加密
C language makes a small maze
【C语言switch分支语句和循环语句】
Redis lock
C语言中堆内存介绍和管理
初识C语言——为什么每个C程序都有一个main函数
Share a multiple-choice question about define (including the replacement rules, program environment and preprocessing related knowledge of define during precompiling)
Introduction to C language
Time complexity and space complexity
初识C语言——什么是C语言
2021 Niuke multi school training camp 5 (question b)
[C language switch branch statement and loop statement]
Selenium element operation
流程控制-分支
Carmaker quick start lesson 4 developing 48V P1 hybrid system
进制的特性









