T
Tony Johansson
Hello!
In this piece of code both bytepek and shortpek works as expected but
longpek and doublepek give
unexpected result. Can somebody explain why not doublepek and longpek works
the same as bytepek and shortpek ?
static unsafe void Main(string[] args)
{
int x = 10;
int* pX = &x;
byte* bytepek = (byte*)pX; //works
short* shortpek = (short*)pX; //works
long* longpek = (long*)pX; //does not work
double* doublepek = (double*)pX; //does not work
Console.WriteLine("Address of x is 0x{0:X}, size is {1}, value is
{2}",
(uint)&x, sizeof(int), x);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&bytepek, sizeof(byte*), (uint)bytepek);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&shortpek, sizeof(short*), (uint)shortpek);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&longpek, sizeof(long*), (uint)longpek);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&doublepek, sizeof(double*), (uint)doublepek);
Console.WriteLine(*bytepek);
Console.WriteLine(*shortpek);
Console.WriteLine(*doublepek);
Console.WriteLine(*longpek);
}
//Tony
In this piece of code both bytepek and shortpek works as expected but
longpek and doublepek give
unexpected result. Can somebody explain why not doublepek and longpek works
the same as bytepek and shortpek ?
static unsafe void Main(string[] args)
{
int x = 10;
int* pX = &x;
byte* bytepek = (byte*)pX; //works
short* shortpek = (short*)pX; //works
long* longpek = (long*)pX; //does not work
double* doublepek = (double*)pX; //does not work
Console.WriteLine("Address of x is 0x{0:X}, size is {1}, value is
{2}",
(uint)&x, sizeof(int), x);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&bytepek, sizeof(byte*), (uint)bytepek);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&shortpek, sizeof(short*), (uint)shortpek);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&longpek, sizeof(long*), (uint)longpek);
Console.WriteLine("Address of is 0x{0:X}, size is {1}, value is
0x{2:X}",
(uint)&doublepek, sizeof(double*), (uint)doublepek);
Console.WriteLine(*bytepek);
Console.WriteLine(*shortpek);
Console.WriteLine(*doublepek);
Console.WriteLine(*longpek);
}
//Tony