To fix the compilation error try
int* p=(int*)0xA0000000;
Whatever this should be good for ...
题
I need to do the following:
Assume that the C++ array
int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };
is stored on a 32-bit machine, at memory address 0xA0000000 (hexadecimal). What are the values of the following expressions?
fib
*fib
&fib[0]
&fib[6]
*(fib+7)
I tried writing this code to try and print out the desired values.
int main()
{
int i = 0;
int *p = &A0000000;
cout << "i= " << i << endl;
cout << "p= " << p << endl;
*p = 0;
cout << "i=" << i << endl;
int &r = i;
r = 0;
cout << "i= " << i << endl;
int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };
cout << "fib= " << fib << endl;
cout << "*(*fib)= " << *fib << endl;
cout << "&fib[0]= " << &fib[0] << endl;
cout << "*(fib+7)= " << *(fib+7) << endl;
cout << "&fib[6]= " << &fib[6] << endl;
system("PAUSE");
return 0;
}
I get the following error message:
c:\users\inquirer\documents\visual studio 2010\projects\fib1\fib1\fib1.cpp(7): error C2065: 'A0000000' : undeclared identifier
Thank you in advance for the help. What do I need to do to print out the correct values?
解决方案
To fix the compilation error try
int* p=(int*)0xA0000000;
Whatever this should be good for ...
其他提示
You cannot store something at an arbitrary virtual memory address.
*p = 0;
will crash at runtime, because 0xa0000000 is probably not a valid location to write to.
Try int *p = new int
and print the address you got. It will be less beautiful than 0xa0000000 but it will be sufficient to visuallise your experiment.