c++ - Assigning value of global pointers -
#include <iostream> int = 9; int *p; p = &a; int fun(); int main() { std::cout << fun(); return 0; } int fun() { return *p; }
why code give error:
expected constructor, destructor, or type conversion before '=' token|
whereas code runs ok:
#include <iostream> int = 9; int *p = &a; int fun(); int main() { std::cout << fun(); return 0; } int fun() { return *p; }
you allowed declare , initialize variables/types globally not assign them.
main()
start of c++ program , assign statements have inside main.
c++03 standard: section $3.6.1/1:
a program shall contain global function called main, designated start of program.
if coming scripting background, should note c++ different scripting languages in way can declare items outside bounds of designated start of program (main()
), cannot processing(assignment or other statements).
Comments
Post a Comment