1

I have a list and I want to create a global counter for each item in the list, with the initial value set to 1. I thought I would do this with a for loop (my actual list is a lot bigger than the example below) however I just realized for loops don't work outside of a function. How can I get the below code to work?

char* list[] = {"A", "B", "C"};

for (int i = 0; i < sizeof(list); i++){
    int counter[i] = 1;
    }
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
david_10001
  • 492
  • 1
  • 6
  • 22
  • Global variables are initialized to 0. All you need to do is declare your variable. – user3386109 May 23 '18 at 05:29
  • the list is inside the function? – Lakshraj May 23 '18 at 05:29
  • Sorry, I used 0 as a bad example. I've changed it to 1. – david_10001 May 23 '18 at 05:31
  • Then you need to put the loop inside a function, e.g. at the top of `main`, or in a separate function that's called at the top of `main`. The declaration can still go outside: `int counter[sizeof(list)/sizeof(list[0])];` – user3386109 May 23 '18 at 05:33
  • Please show examples of how you intend to use the counters. You are going to implement many pieces of code, each of which use one of the counters differently? `counter[A]++`? `counterB++`? `counter['A']++`? `C++`? `x='D'; counter[x]++`? – Yunnosch May 23 '18 at 05:34
  • Please explain more about `list`, the array of strings, and its meaning for your purpose? How is the list of words related to what you intend to do? – Yunnosch May 23 '18 at 05:39
  • Do you want those many global counters to be initialised with equally many explicit initialiation values? Or do you want to initialise all of them programatically, all of them with the same init value? – Yunnosch May 23 '18 at 05:41
  • Can you comapre what you want to achieve to constructs from other programming languages? E.g. "map", perls "hash"... ? – Yunnosch May 23 '18 at 05:43
  • Is the list of strings going to be as systematically predictable as in your example? E.g. will the next be "D"? Or could there be "Z", "M", "Peter", "John Skeet", "*+-&"? – Yunnosch May 23 '18 at 05:52

2 Answers2

4

is this what you are after?

char* list[] = {"A", "B", "C"};
int counter[] = {1, 1, 1};

Just declare them both globally and initialize counter just like you initialize list.

It is also possible to declare counter globally and then initialize it in main() or wherever with a for loop:

char* list[] = {"A", "B", "C"};
int counter[3];

int main(void) {
    for (int i = 0; i < sizeof(list) / sizeof(*list); i++) {
        counter[i] = 1;
    }
}

or even better, figure out how much memory you need for counter based on the size of list:

char* list[] = {"A", "B", "C"};
int *counter;

int main(void) {
    int numElements = sizeof(list) / sizeof(*list);

    counter = malloc(numElements * sizeof(*counter));
    // check for malloc() failure

    for (int i = 0; i < numElements; i++) {
        counter[i] = 1;
    }
}

Notice that you can't simply use sizeof(list)...that will return how much memory list has allocated. You need to divide sizeof(list) by the size of each element, sizeof(*list), to get the number of elements

alk
  • 69,737
  • 10
  • 105
  • 255
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
3

C has no facility to run such initialization code outside of functions. And since initializers to static variables at file scope need to be constant expressions, you have two options:

  1. Create a script in your scripting language of choice that generates an initializer macro of the right size, for you to use as an initializer to the array. For instance, the script can read this file:

    #include "my_init_list.h"
    char* list[] = {"A", "B", "C", /* However many more */};
    int counter[] = COUNTERS_INITIALIZER;
    

    It would parse the initializer for list, and output the file my_init_list.h which would contain the macro COUNTERS_INITIALIZER that is expanded to {1, 1, ..., 1}. If you run the script as a step in your build process, that should do it.

  2. Put the initialization code into a function, and rig your function to run once early in your program's startup code (what main executes when it starts running).

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458