#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x = 65;
int *ptr = &x;
char * a= (char *)ptr;
cout<<(int)*(a);
getch();return 0;
}
Sixeof(ptr) 和 Sizeof(a) 显示 4
Sizeof(int) 显示 4 而 sizeof(char) 显示 1
所以 65 存储在 4 个字节中,即
00000000 00000000 00000000 01000001 并且第一个字节的地址存储在 ptr
在上面的代码中,我将 int* 类型转换为 char* 以打印存储在 x(type int) 第一个字节中的值。
因此,在类型转换后“a”存储第一个字节地址,即包含在 ptr 中现在在显示 (int)*a 时,它应该只考虑第一个字节来显示值..??但输出是 65 而不是 0(第一个字节值)..我哪里错了..?
我学到的是
char * ptr1;
ptr1++; //Ptr1 goes to the next byte..*ptr1 will display only 1 byte value
int * ptr2;
ptr1++; //Ptr2 goes to the next 4 byte..*ptr2 will display value conmtain in 4 bytes
PS - 我正在开发 Dev-C++