6

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
cdub
  • 61
  • 1
  • 1
  • 2

8 Answers8

11

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.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • So there is no way other than that for me to set up something that will do cc_no_issue[0]=0 cc_no_issue[1]=0 cc_no_issue[2]=0 cc_no_issue[3]=0?? – cdub Aug 09 '11 at 04:49
  • ziyao thanks for responding....my problem though is in a seperate function I want to increment these all of these values depending on what my k value would be. for example...if i want to increment cc_no_issue[k]++; This k value can dynamically change from 0-3 so i want to make sure it increments the proper one. I do see that you have set 100 elements to zero there – cdub Aug 09 '11 at 05:03
  • Sorry for my misunderstanding...I guess it really does not matter if i define 100 or 100 elements.... When i use K in a function down below no matter if its 1 or 100 it will increment the proper element in that array...Is this correct? – cdub Aug 09 '11 at 05:04
  • @cdub: I'm not sure if I understand you, but I think it should be okay. The 100 is just the size, you could control any element of the array just as normally you'll do. The initialization just set all the unspecified element to 0. – zw324 Aug 09 '11 at 05:08
  • @cdub Your question is about initialization of a global array. Then you suddenly involve a dynamically changing variable. These two things speak against each other: initialization is done statically before the variable is used. If things need to be changed through a dynamic variable, you are no longer initializing but assigning values. In fact you are likely asking about the latter, not the former, and therefore you will likely get confusing answers in return. – Lundin Aug 09 '11 at 08:20
5

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
}
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
3

All global variables (variables at file scope) are by default initialized to zero since they have static storage duration (C99 6.7.8.10). So strictly speaking, you needn't initialize them to zero, the C standard guarantees that they are zero by default.

It is good programming practice to initialize them explicitly however, as mentioned in the answer by Ziyao Wei.

Lundin
  • 195,001
  • 40
  • 254
  • 396
2

No, you can't have code outside of functions.

You can put it inside some function and call that from the start of main.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Ugh...So there is no way other than that for me to set up something that will do cc_no_issue[0]=0 cc_no_issue[1]=0 cc_no_issue[2]=0 cc_no_issue[3]=0?? – cdub Aug 09 '11 at 04:48
  • There is (I only answered half the question). Ziyao showed how to set all values to zero. You can also have different values for different array elements, like `{1,2,3,4,5}`. – Bo Persson Aug 09 '11 at 04:54
2

One way is to add a global function that:

  • Checks if the array is initialized
  • Initializes the array if it wasn't initialized
  • Returns the array

Example Code:

int* get_cc_no_issue()
{
  if(!kIsMyArrayInitialized)
  {
    /* todo: somehow get "context" globally... */

    for(int k = 0; k < context.size(); k++)
    {
      cc_no_issue[k] = 0;
    }

    kIsMyArrayInitialized = true;
  }

  return cc_no_issue;
}

This is most useful if you want non-zero initialization.

For zero-initialization, see this answer to another question: Is global memory initialized in C++?

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
0

As @Bo Persson, do it in a function instead. But, there is already an algorithm that does it for you in C++. No need to write a hand written loop.

std::fill(cc_no_issue, cc_no_issue+context.size(); 0) ;

More info on std::fill


Response to your comment:

To increment every element, you can make use of std::for_each passing a function object as the third argument.

#include <iostream>
#include <algorithm>

using namespace std;

void incrementHelper( int& a ){
        ++a;
}

int main(){

        int ar[] = { 1,2,3,4,5 };
        for_each(ar, ar+5, incrementHelper );

        for( int i=0; i<sizeof(ar)/sizeof(ar[0]); ++i ){
                cout << ar[i] << endl;
        }
        return 0;
}

Ouput:

2
3
4
5
6

for_each(ar, ar+5, incrementHelper );

For each element in the array, the algorithm is going to call the function, incrementHelper. In C terminology,to say, it serves as a call back for each element in the array. Now the call back function, receives the passed element by reference. So, modifying the reference will modify the referrent also. See the online demo.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • I am very new to c++ can you tell me with this...how will i be able to increment this value for each element. example I want to do this cc_no_issue[k]++; – cdub Aug 09 '11 at 04:54
0

You can put the array in the constructor of a global object.

int cc_no_issue[256];
struct Init {
  Init(int a, unsigned int size)
  {
    memset(a, 0, size);
  }
};
Init arr(cc_no_issue, sizeof(cc_no_issue));
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Putting the array initialization in constructor of some global object just for the sake of getting it initialized globally doesn't make much of an sense, especially when one can acheive the same in other possible ways without making such unnecessary strong coupling. – Alok Save Aug 09 '11 at 04:55
  • @Als, `Init` constructor can further be generalized to accept any type and any size with using `template`. Anyways, this is just one of the ways. Since other ways are already mentioned in other answers. – iammilind Aug 09 '11 at 05:00
0

You need to decide on the language. The machanisms for this are different in C and C++. Basically C has no method of running code before your main function starts, so you cannot do complex initialisation of an array in C. In C++ you do have some options, one is to stop using a bare array (which is a C construct anyway) and instead wrap your array inside a class, and do the initialisation inside the class constructor.

CC cc_no_issue;

class CC
{
public:
  CC()
  {
    // initialisation of array goes here
  }
private:
  int array[100];
};

Another way it to use a vector, and write a function to initialise the vector.

std::vector<int> cc_no_issue = init_vector();

std::vector<int> init_vector()
{
  std::vector<int> tmp;
  // initialisation of tmp goes here
  return tmp;
}
john
  • 85,011
  • 4
  • 57
  • 81