I have problem creating global variable inside function, this is simple example:
int main{
int global_variable; //how to make that
}
This is exactly what I want to do:
int global_variable;
int main{
// but I wish to initialize global variable in main function
}
You have two problems:
main
is not a loop. It's a function.
Your function syntax is wrong. You need to have parentheses after the function name. Either of these are valid syntaxes for main
:
int main() { } int main(int argv, const char* argv[]) { }
Then, you can declare a local variable inside main
like so:
int main() {
int local_variable = 0;
}
or assign to a global variable like so:
int global_variable;
int main() {
global_variable = 0;
}
There is no way to declare it the way you want. And that's it.
But:
main
body but assign a value to it inside main
. Look Paul's answer for thatint global_variable;
int main()
{
global_variable=3; // look you assigned your value.
}
嗯……通过声明全局指针,然后将局部变量分配给它们,间接可能是可行的,但有时可能会导致无法访问指向变量的情况。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.