当前位置:网站首页>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
边栏推荐
- Usage of functions decode() and replace() in SQL
- [record] typeerror: this getOptions is not a function
- How can programmers quickly develop high-quality code?
- Tangle
- FTP, SSH Remote Access and control
- 网络协议之:sctp流控制传输协议
- ESP32 LVGL8. 1 - arc (arc 19)
- Minesweeping II of souI instance
- The corresponding permissions required to automatically open the app in the setting interface through accessibility service
- Click the input box to pop up the keyboard layout and move up
猜你喜欢
Introduction to ROS learning notes (II)
Summary of actual business optimization scheme - main directory - continuous update
One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
[mathematical modeling] - analytic hierarchy process (AHP)
Introduction to micro build low code zero Foundation (lesson 3)
浅谈c语言指针的强制转换
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
ESP32 LVGL8. 1 - textarea text area (textarea 26)
Wechat video extraction and receiving file path
redis优化系列(三)解决主从配置后的常见问题
随机推荐
mysql_linux版本的下载及安装详解
Advanced transfer learning
MVVM model
Feature selection feature_ selection--SelectKBest
ESP32 LVGL8. 1 - roller rolling (roller 24)
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
7、 DOM (Part 2) - chapter after class exercises and answers
Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
Simplified path (force buckle 71)
简化路径(力扣71)
Fundamentals of machine learning theory -- some terms about machine learning
Machine learning theory (8): model integration ensemble learning
Practice of Druid SQL and security in meituan review
Use of kotlin collaboration in the project
mysql_linux版本的下載及安裝詳解
Chondroitin sulfate in vitreous
剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
How can programmers quickly develop high-quality code?
ESP32 LVGL8. 1 - msgbox message box (msgbox 28)