当前位置:网站首页>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
 Insert picture description here
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 :
 Insert picture description here
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 ?
 Insert picture description here
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
 Insert picture description here
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
 Insert picture description here
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 :
 Insert picture description here
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