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;
    }
}
9
  • Global variables are initialized to 0. All you need to do is declare your variable. Commented May 23, 2018 at 5:29
  • the list is inside the function? Commented May 23, 2018 at 5:29
  • Sorry, I used 0 as a bad example. I've changed it to 1. Commented May 23, 2018 at 5: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])]; Commented May 23, 2018 at 5: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]++? Commented May 23, 2018 at 5:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

sizeof(list)/sizeof(*list) is a constant expression and can be used as size for counter as int counter[sizeof(list)/sizeof(*list)];
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.