#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有趣的问题帮助
在上面的代码我有型铸造整型*为char *在一个动机来打印第一个字节存储在X(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 - 我正在开发-C++工作
sizeof(char)总是1,根据定义 – 2010-09-06 12:52:33