简体   繁体   English

在c ++中从int *转换为char *

[英]Casting from int* to char* in c++

  • line 1 prints 'X' 第1行打印'X'
  • line 2 prints 'c' 第2行打印'c'
  • line 3 prints 11 第3行打印11
  • line 4 prints 5. 第4行打印5。

I understand these lines but why does 我明白这些线,但为什么呢

  • line 5 print 20 第5行打印20

?

Should it not print 11 instead of 20 since line 1 and 2 print 'X' and 'c'. 它不应该打印11而不是20,因为第1行和第2行打印'X'和'c'。

Your help is much appreciated. 非常感谢您的帮助。

#include "iostream"
using namespace std;
int main()
{
int arr[] = {88, 20, 30, 40, 50, 99};
int *ptr1 = arr;
int *ptr2 = arr + 5;
cout<<(char*)ptr1<<endl;//line 1
cout<<(char*)ptr2<<endl;//line2
cout<<('c'-'X')<<endl;//line3
cout<<ptr2-ptr1<<endl;//line4
cout<<(char*)ptr2-(char*)ptr1<<endl;//line5

return 0;
}

In the environment where the program was compiled sizeof( int ) is equal to 4 . 在编译程序的环境中, sizeof( int )等于4 That is each object of type int occupies 4 bytes (characters). 也就是说int类型的每个对象占用4个字节(字符)。 Between ptr2 and ptr1 there are 5 integers. 在ptr2和ptr1之间有5个整数。 The difference of two pointers that point to elements of the same array is the number of elements between them. 指向同一数组的元素的两个指针的差异是它们之间的元素数量。 It is so-called pointer arithmetic. 它就是所谓的指针算法。 Thus ptr2 - ptr1 gives 5. But if you will cast these pointers to type char * then between these pointers there are 5 * sizeof( int ) characters that is equal to 20. 因此ptr2 - ptr1给出了5.但是如果你将这些指针转换为char *类型,那么在这些指针之间有5 * sizeof( int )字符等于20。

Because of (char*)ptr2-(char*)ptr1 is the difference of addresses. 因为(char*)ptr2-(char*)ptr1是地址的差异。 Then 5*sizeof(int) = 20 in your (I guess) 32 bits platform. 然后在你的(我猜)32位平台上5*sizeof(int) = 20

要获得您期望使用的输出:

(char)*ptr2-(char)*ptr1

The answer is absolutely correct let me explain your anwer step by step according to code: 答案绝对正确让我按照代码逐步解释你的答案:

cout<<(char*)ptr1<<endl;//line1

Since ptr1 = arr, therefore first char is 88 and thus printed X 由于ptr1 = arr,因此第一个char为88,因此打印X.

cout<<(char*)ptr2<<endl;//line2

Since ptr1 = arr +5, therefore fifth char is 99 and thus printed c 由于ptr1 = arr +5,因此第五个字符为99,因此打印c

cout<<('c'-'X')<<endl;//line3

Since the value of 'c' =99 and 'X' = 88 thus answer is 11 由于'c'= 99和'X'= 88的值因此答案是11

cout<<ptr2-ptr1<<endl;//line4

Since the arr is an array and distance between 5th and 0th element 5-0 = 5 由于arr是一个数组,第5和第0个元素之间的距离为5-0 = 5

cout<<(char*)ptr2-(char*)ptr1<<endl;//line5

What here happens there you are subtracting two address and answer is provided as differance of two addresss, for instance 这里发生了什么,你减去两个地址,答案是作为两个地址的差异提供的,例如

 (char *)ptr1 --- Address = 10
 (char *)ptr2 --- Address = 30

and if we subtract both then 30-10=20. 如果我们减去两者,那么30-10 = 20。 Why it is 20 as it depends on the sizeof void * 为什么它是20,因为它取决于void的大小*

32 bit sizeof void* = 4
64 bit sizeof void* = 8

Thus on 32 bit system answer would be 4*5=20 and on 64 bit system it would be 8*5=40 因此,在32位系统上,答案将是4 * 5 = 20,而在64位系统上,它将是8 * 5 = 40

The first ptr1 points to these bytes in memory: 88,0,0,0 - which is the first index in array. 第一个ptr1指向内存中的这些字节:88,0,0,0 - 这是数组中的第一个索引。
The ptr2 points to these bytes in memory: 99,0,0,0. ptr2指向内存中的这些字节:99,0,0,0。
So the first two lines are outputting two null terminated strings: X and c. 所以前两行输出两个空终止字符串:X和c。

The third line is outputting the difference between chars c and X, which is int value. 第三行输出字符c和X之间的差值,即int值。

The fourth value is difference between two pointers in respect to on what these pointers are pointing to (relative position in array). 第四个值是关于这些指针指向的两个指针之间的差异(数组中的相对位置)。 This is an int number. 这是一个int号。

The last is cast to char*, where char is 1 byte, which makes it 4 times smaller then int (int is 4 bytes big). 最后一个是转换为char *,其中char是1个字节,这使得它比int小4倍(int是4个字节大)。 So the difference is (array position difference) * (size of int) = 5 * 4 = 20. This value is int so that is why you see number 20. 所以不同的是(数组位置差异)*(int的大小)= 5 * 4 = 20.这个值是int,这就是你看到数字20的原因。

Try to change all those ints in to char and see the difference: 尝试将所有这些内容更改为char并查看区别:

char arr[] = { 88, 20, 30, 40, 50, 99 };
char *ptr1 = arr;
char *ptr2 = arr + 5;

Suddenly these first two lines are no longer one chars, because there is no longer the 0 terminator for (char*) strings. 突然,前两行不再是一个字符,因为(char *)字符串不再有0个终止符。

X¶▲(2c╠╠╠╠╠╠╬H@▼ñ¸4 X¶▲(2c╠╠╠╠╠╠╬H@▼N4
c╠╠╠╠╠╠╬H@▼ñ¸4 c╠╠╠╠╠╠╬H@▼N4
11 11
5
5

This will vary between debug and release. 这将在调试和发布之间变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM