I'm trying to do this:
for(int k=0; k<context.size(); k++)
{
cc_no_issue[k]=0;
}
Can someone tell me how I can do that globally? Whenever I try I get these errors:
expected unqualified-id before "for"
k does not define a type
k does not define a type
The arrays can be declared and initialized globally as well as locally(i.e., in the particular scope of the program) in the program.
As in case of scalar variables, we can also use external or global arrays in a program, i. e., the arrays which are defined outside any function. These arrays have global scope. Thus, they can be used anywhere in the program.
This will do:
long cc_no_issue[100]={0};
And this is the proper initialization.
Note: this will initialize all the contents to 0.
This sentence:
long cc_no_issue[100]={1,2};
will set cc_no_issue[0]
to 1, cc_no_issue[1]
to 2, and the rest to 0. You could see the link above for more information.
If you have a global array of a basic type,
int some_array[1000];
It will automatically be initialized to zero. You do not have to initialize it. If you do need to run initialization code, you can do a hack like the following (in C++):
struct my_array_initializer {
my_array_initializer() {
// Initialize the global array here
}
};
my_array_initializer dummy_variable;
If you are on GCC (or Clang), you can execute code before main
with the constructor
attribute:
__attribute__((constructor))
void initialize_array()
{
// Initialize the global array here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With