-1
vector<string> commonChars(vector<string>& words) {
    int count[words.size()][26] = {}; 
    //getting error 
    //error: variable-sized object may not be initialized

    string ele;
    vector<string> result;
    for(int s1=0; s1<words.size(); s1++){
        for(int s2=0; s2<words[s1].size(); s2++){
            count[s1][words[s1][s2]-97]++;
        }
    }
    for(int s=0; s<26; s++){
        int small = count[0][s];
        for(int i=1; i<words.size(); i++){
            small = min(small, count[i][s]);
        }
        while(small-- > 0){
            ele = (char)(s+97);
            result.push_back(ele);
        }
    }
    return result;
}

On the line int count[words.size()][26] = {};

error: variable-sized object may not be initialized

Can anyone help me? What is this error?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 8
    Variable length arrays like `int count[words.size()]` are a non-standard compiler extension. `count` should probably be a `vector` instead. – 0x5453 Jun 04 '21 at 18:52
  • 1
    I recommend upgrading your compiler. That issue was resolved in GCC 4.9 and the cool kids are using GCC 11.1 these days. In GCC 4.8 and earlier variable length arrays did not zero initialize. – user4581301 Jun 04 '21 at 18:55
  • 3
    @0x5453 or, in this case, a `std::vector>` instead. – Remy Lebeau Jun 04 '21 at 18:59
  • @user4581301 Well, that's a compiler extension of a compiler extension: https://godbolt.org/z/xY6dnsze3 – Bob__ Jun 04 '21 at 20:38
  • Well well welly well. When I was playing with this yesterday I didn't specify the Standard. Looks like there are many shades to this bit of non-Standard mess. I'm just going to stick with not using the damn things. – user4581301 Jun 04 '21 at 20:43
  • Wait a second. One of those is using a C compiler. No fair! – user4581301 Jun 04 '21 at 20:45
  • Does this answer your question? [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Cortex0101 Jun 05 '21 at 05:28

1 Answers1

1

This declaration

int count[words.size()][26] = {};

declares a variable length array because the value of the expression words.size() is not a compile time constant.

Variable length arrays is not a standard C++ feature. It seems that the used compiler has its own language extension that allows to use VLAs.

Nevertheless you may not initialize such an array in its declaration.

So you need to write

int count[words.size()][26];

Then you can use for example either the C function memset or a standard algorithm like std::fill or std::generate to initialize the array.

In fact there is no need to declare a two-dimensional variable length array in your function. At least you could use a one-dimensional array like

int count[26] = {};

and use it for processing each string in the vector.

But it would be even better to use the standard container std::map<char, size_t> instead of the array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335