当前位置:网站首页>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
边栏推荐
- The corresponding permissions required to automatically open the app in the setting interface through accessibility service
- Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
- Methods of nested recycleview to solve sliding conflict and incomplete item display
- [popular science] CRC verification (I) what is CRC verification?
- Switching power supply design sharing and power supply design skills diagram
- 在渤海期货办理开户安全吗。
- Using Visual Studio code to develop Arduino
- c1000k TCP 连接上限测试1
- Seata handles distributed transactions
- 开关电源设计分享及电源设计技巧图解
猜你喜欢

MySQL Téléchargement et installation de la version Linux

开关电源设计分享及电源设计技巧图解

FTP、ssh远程访问及控制

Machine learning practice - naive Bayes

Android Development: the client obtains the latest value in the database in real time and displays it on the interface
![[记录]TypeError: this.getOptions is not a function](/img/c9/0d92891b6beec3d6085bd3da516f00.png)
[记录]TypeError: this.getOptions is not a function

Machine learning theory (8): model integration ensemble learning

Practice of Druid SQL and security in meituan review

ESP32 LVGL8. 1 - msgbox message box (msgbox 28)

Simplified path (force buckle 71)
随机推荐
Fundamentals of machine learning theory -- some terms about machine learning
Machine learning theory (8): model integration ensemble learning
WebView opens H5 video and displays gray background or black triangle button. Problem solved
Nacos作为服务注册中心
About the operation of unit file reading (I)
Eight bit binary multiplier VHDL
[mathematical modeling] - analytic hierarchy process (AHP)
Practice of Druid SQL and security in meituan review
Methods of nested recycleview to solve sliding conflict and incomplete item display
Getting started with vcpkg
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
7、 DOM (Part 2) - chapter after class exercises and answers
Some records used by VS2010
One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
高层次人才一站式服务平台开发 人才综合服务平台系统
Machine learning practice - naive Bayes
binlog2sql 工具安装使用及问题汇总
An 8266 crash
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题