简体   繁体   English

C++ 中指针的类型转换

[英]Typecasting of Pointers in C++

Assume pointer size is 4假设指针大小为 4

#include<stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr1 = arr;
    int *ptr2 = arr + 5;
    printf("Number of bytes between two pointers are: %d", 
                        (char*)ptr2 - (char*) ptr1);
    return 0;
}

Output : 20输出:20

Here in this program why in typecasting it is printing the output as sizeof pointer?在这个程序中,为什么在类型转换中将输出打印为 sizeof 指针? Why it's not printing 5?为什么不打印 5?

Here in this program why in typecasting it is printing the output as sizeof pointer?在这个程序中,为什么在类型转换中将输出打印为 sizeof 指针? Why it's not printing 5?为什么不打印 5?

The cast to char* does that.char*的演员表就是这样做的。

The offset between ptr2 and ptr1 is 5 as long as the pointers are of type int* .只要指针是int*类型, ptr2ptr1之间的偏移量就是5 When you explicitly cast the pointers to char* , you are asking the program to compute the offset between the pointers as if they were char* .当您显式地将指针强制转换为char* ,您是在要求程序计算指针之间​​的偏移量,就好像它们是char* Your program says, it's 20 , which is equal to 5*sizeof(int) .你的程序说,它是20 ,它等于5*sizeof(int)

Each element of the array occupies 4 bytes of space in memory.数组的每个元素在内存中占用 4 个字节的空间。

Therefore, the calculation can be done as follows:因此,计算可以如下进行:

  • Bytes between pointers = char_type_cast of (pointer2 - pointer1)指针之间的字节数 = char_type_cast of (pointer2 - pointer1)
  • Pointer-sized spaces = (bytes_between_pointers / 4)指针大小的空格 = (bytes_between_pointers / 4)
  • Integer-sized spaces = int_type_cast of (pointer2 - pointer1)整数大小的空格 = int_type_cast of (pointer2 - pointer1)

Working code:工作代码:

#include<stdio.h>
int main()
{
    int arr[] = {10, 20, 30, 40, 50, 60};
    int *ptr1 = arr;
    int *ptr2 = arr + 5;
    
    
    /* 
      1 pointer = 4 bytes 
  
      Byte-sized spaces = char_type_cast of (ptr2 - ptr1)
        
      Pointer-sized spaces = byte_spaces / 4
        
      Integer-sized spaces = int_type_cast of (ptr2 - ptr1)
    */
  
    int byteSpaces = ((char*)ptr2 - (char*)ptr1);
    int pointerSpaces = (byteSpaces / 4);
    int integerSpaces = ((int*)ptr2 - (int*)ptr1);
    
    
    printf("Pointer 1 = %d\n", *ptr1);
    printf("Pointer 2 = %d\n", *ptr2);
    printf("\nByte sized spaces between two pointers = %d\n", byteSpaces);
    printf("\nPointer sized spaces between pointers = %d\n", pointerSpaces);
    printf("\nInteger sized spaces between pointers = %d\n", integerSpaces);
    return 0;
}

Output:输出:

Pointer 1 = 10
Pointer 2 = 60

Byte sized spaces between two pointers = 20

Pointer sized spaces between pointers = 5

Integer sized spaces between pointers = 5

Apart from @R sahu's answer, take a look at this code and run it through a debugger.除了@R sahu 的回答,看看这段代码并通过调试器运行它。 You will find that t1 = '\\n' and t2 = '<' .你会发现t1 = '\\n't2 = '<' Subtraction is 20减法是20

Code:代码:

#include<stdio.h>
#include <iostream>
int main() {
    int arr[] = { 10, 20, 30, 40, 50, 60 };
    int* ptr1 = arr;
    int* ptr2 = arr + 5;
    auto* t1 = ( char* ) ptr1;
    auto* t2 = ( char* ) ptr2;
    std::cout << ( int ) (t1 - t2);
    printf("Number of bytes between two pointers are: %d",
        ( char* ) ptr2 - ( char* ) ptr1);
    return 0;
}

When you do arithmetic on pointers, it operates in multiples of the pointed-to type's size.当您对指针进行算术运算时,它以指向类型大小的倍数进行运算。 For that reason, the following assignments are equivalent:因此,以下分配是等效的:

int* ptr2 = arr + 5;
int* ptr2 = &arr[5];

Either way, it's a pointer to the 6th element in the array arr .无论哪种方式,它都是指向数组arr第 6 个元素的指针。

As the int s are each evidently 4 bytes in side, when you cast the pointers to char* s and subtract one from the other, the distance between the pointers is calculated in multiples of the new pointed-to type - char 's size, which is one.由于int s 每个显然是 4 个字节,当您将指针转换为char* s 并从另一个中减去一个时,指针之间的距离以新指向的类型的倍数计算 - char的大小,这是一个。 That difference between the 6th int and start of arr is 5 * sizeof(int) or 20 bytes.第 6 个intarr开始之间的差异是5 * sizeof(int)或 20 个字节。

For some background reading on pointers - consider my answer here .有关指针的一些背景阅读 -在这里考虑我的回答

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

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