Code:
#include <cstdlib>
#include <iostream>
#define PI 3.14159
using namespace std;
int main(int argc, char** argv) {
cout<<"Address of PI:"<<&PI<<endl;
return 0;
}
Here is the output:
main.cpp: In function int main(int, char**)':&’
main.cpp:20: error: non-lvalue in unary
make[2]: * [build/Debug/Cygwin-Windows/main.o] Error 1
make[1]: [.build-conf] Error 2
make: ** [.build-impl] Error 2
So why can’t I see the memory address of PI here?
Thank you.
Macros are never allocated a memory. Before the code is compiled, the compiler does a text search in the file and replace all Macros with their value. Also this is a text search, so the text gets replaced. So PI gets replaced by 3.14 in your code before compiling it. As a result, the memory operator throws an error because it cannot get the value of 3.14 as it is not a variable. Hope it helps 🙂