当前位置:网站首页>On the forced conversion of C language pointer
On the forced conversion of C language pointer
2022-04-23 19:06:00 【*Temporary】
Pointer is c The soul of language , The forced conversion of data is a means we often use in the process of writing programs , So what will be the effect of the combination of the two ?
Let's just take an example
No.1
The above is a simple program to print out variables , Displays the value of the pointer to the address , I won't go into that . Now let's add a few lines of program :
We know ,char The type is signed , So let's first define an initial value as -100, The printed value is also correct -100, This is an additional definition of unsigned char Pointer variables of pointer type , And then put char The variable of the pointer is cast and assigned to unsigned Insert pointer variables of type , Then print out the value of the variable of this address , It's different from the above .
Of course it will be different ,unsigned char It's said to be unsigned , So I'm sure I won't print negative numbers . So why is it the same address , The printed values will be inconsistent ?
We know , The form of data stored in the computer is binary , In other words, it is a string of 1 and 0 The composition of the data ,char type , So that is 8 position , and -100, This string of data is 10011100 . In other words, the above string of data is stored in the computer , And was marked char The label of , So when we call , Computer can use char The way to interpret 10011100 This string of data , That's why it prints out -100 了 .
And then , We will char The pointer variable of is cast and assigned to unsigned char Pointer variable for , Explain what , It means that I created a new pointer variable to point to this address and marked unsigned char This tag , So next time I call this pointer variable c When , Will use unsigned char To interpret , therefore 10011100 The interpretation will be 156.
Above is the first usage , The forced conversion of pointer is to label the data of the same address with other types of data and read it with that type of data , To deal with .
Take a more vivid chestnut :
No.2
Define a floating point number 10.25, And one. float The type and int Pointer variable of type , I am a 64 Bit operating system ,int Occupy 32 position , and float equally . about float The storage form in the computer can refer to this article Decimal and binary conversion of floating point numbers , I won't go into details here .10.25, Stored in the computer is :0100 0001 0010 0100 0000 0000 0000 0000 Is the total 32 position , Well, according to the above idea , I'll give him this address int The label of , Then the above string of data is used int How much is it , The answer is 1092878336.
Another use , You must have found that the length of the data in the above examples is the same ,8 position ,32 position , What if the data length is different , This is another form , Please see the example :
No.3
I defined a char Type of ,size yes 16 And print out the first few data ,p_a + 1 That is, point the pointer to the next address , Because the address of the array is continuous , So the data pointing to the next unit , That is, the next data of the array is printed out , This is no problem , At this time, I type this address int Assign a label to int Pointer to type , Then add... To it 1 Printing operation , It shows 4 A seemingly messy value , This is not random code , So how do these four values come from ? You can list the address :
We combine int The type is occupied 4 A byte of knowledge , Give him the first four bytes as a variable , It is found that the fourth data to the first data are concatenated from high position to position ,0 1 0 1( Decimal system ) After the conversion is 00000000 00000001 00000000 00000001, So this data is 65537, So the second number 131074 Well , Yes, it is to string the eighth data to the fifth data , And so on .
From the above example, we can see , The pointer +1 In terms of data unit size , After strong conversion, the unit size of data will change , So every time +1 The post address jump is also different .
版权声明
本文为[*Temporary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231904324615.html
边栏推荐
猜你喜欢
Android Development: the client obtains the latest value in the database in real time and displays it on the interface
Chondroitin sulfate in vitreous
Esp32 (UART 485 communication) - 485 communication of serial port (3)
解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
【历史上的今天】4 月 23 日:YouTube 上传第一个视频;网易云音乐正式上线;数字音频播放器的发明者出生
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
2022.04.23(LC_763_划分字母区间)
ESP32 LVGL8. 1 - checkbox (checkbox 23)
7、 DOM (Part 2) - chapter after class exercises and answers
随机推荐
Some records used by VS2010
ESP32 LVGL8. 1 - checkbox (checkbox 23)
Redis common interview questions
Nacos集群搭建和mysql持久化配置
Seata handles distributed transactions
Introduction to ROS learning notes (I)
机器学习理论基础篇--关于机器学习的一些术语
SSDB基础
Simple use of viewbinding
std::stoi stol stoul stoll stof stod
【历史上的今天】4 月 23 日:YouTube 上传第一个视频;网易云音乐正式上线;数字音频播放器的发明者出生
Scrollto and scrollby
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
在渤海期货办理开户安全吗。
Nacos作为服务注册中心
Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
: app: transformclasseswithrobustfordevrease meituan hot repair compilation error record
Advanced transfer learning
Fundamentals of machine learning theory -- some terms about machine learning
ESP32 LVGL8. 1 - textarea text area (textarea 26)