当前位置:网站首页>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
边栏推荐
- #yyds干货盘点#stringprep --- 因特网字符串预备
- RPM包管理
- Fundamentals of machine learning theory -- some terms about machine learning
- Some records used by VS2010
- [record] typeerror: this getOptions is not a function
- [advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
- 2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
- Esp32 (UART event) - serial port event learning (1)
- Introduction to micro build low code zero Foundation (lesson 3)
- 12个例子夯实promise基础
猜你喜欢
redis优化系列(三)解决主从配置后的常见问题
Keysight has chosen what equipment to buy for you
JVM的类加载过程
Raspberry pie 18b20 temperature
WebView opens H5 video and displays gray background or black triangle button. Problem solved
简化路径(力扣71)
Esp32 (UART 485 communication) - 485 communication of serial port (3)
Simplified path (force buckle 71)
mysql通过binlog恢复或回滚数据
Some records used by VS2010
随机推荐
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
腾讯云GPU最佳实践-使用jupyter pycharm远程开发训练
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
SSDB基础3
SSDB基础
Accessing private members using templates
Seata处理分布式事务
ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
How about CICC wealth? Is it safe to open an account up there
Actual combat of Nacos as service configuration center
特征选择feature_selection--SelectKBest
Esp32 (UART event) - serial port event learning (1)
Introduction to ROS learning notes (I)
Nacos集群搭建和mysql持久化配置
redis优化系列(三)解决主从配置后的常见问题
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
Client interns of a large factory share their experience face to face
Usage of functions decode() and replace() in SQL
Simple use of navigation in jetpack
MVVM模型