#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;
} 六开(Ptr)和六开(A)显示器4
Sizeof(int)显示4,sizeof(char)显示1
因此65是以4字节存储的,即
00000000 00000000 00000000 01000001,第一个字节的地址存储在ptr中
在上面的代码中,我将int*类型转换为char*,目的是打印存储在x(int类型)第一个字节中的值。
因此,在类型转换"a“存储ptr中包含的第一个字节地址ie之后,现在在显示(int)*a时,是否应该只考虑第一个字节来显示值..??但是输出是65而不是0(第一个字节值)..Where我错了吗?
我学到的是
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 bytesPS -我正在开发Dev-C++
发布于 2010-09-06 20:49:19
你的机器是little-endian的,最不重要的字节在前面。
https://stackoverflow.com/questions/3651539
复制相似问题