0

Why the below piece is a valid C++ code but an - invalid C code?

int main(){
    unsigned int const size_of_list = 20;
    const char* list_of_words[size_of_list] = {"Some", "Array"};

    for (unsigned int writing_index = 0; writing_index < size_of_list; writing_index ++)
        ;
        return 0;
}

enter image description here

thumala manish
  • 198
  • 1
  • 10
  • Try `const unsigned int size_of_list = 20;` – Irelia Dec 28 '21 at 17:55
  • So your question is why does the `c` compiler reject this code but the `c++` compiler accept? – drescherjm Dec 28 '21 at 17:55
  • 2
    @Irelia The order of qualifiers doesn't matter. – Barmar Dec 28 '21 at 17:55
  • 3
    The error message is pretty clear. You're allowed to declare variable-length arrays, but you can't use an initializer with them. – Barmar Dec 28 '21 at 17:56
  • @Barmar Ah okay. I assumed the const was getting ignored – Irelia Dec 28 '21 at 17:57
  • 2
    This question may need more focus if you are really asking the two questions "Why is this valid C++?" and "Why is this invalid C?" Otherwise there could be a near-infinite number of questions like this. – Drew Dormann Dec 28 '21 at 17:58
  • @DrewDormann The title seems to indicate that the emphasis is on C. – Barmar Dec 28 '21 at 18:10
  • @FrançoisAndrieux I reopened it, but there's almost certainly a duplicate for that question as well. – Barmar Dec 28 '21 at 18:11
  • Just for general interest: gcc will compile the posted code, albeit with a warning of "variable length array folded to constant array as an extension [-Wgnu-folding-content]" if you specify -pedantic on the compile line. – Jeremy Friesner Dec 28 '21 at 19:48

1 Answers1

3

In C:
20 is a constant.
unsigned int const size_of_list is not a constant.

Title: "Why we cannot create an array with a constant in c" does not apply to this code.

const char* list_of_words[size_of_list] = {"Some", "Array"}; // Bad

An issue here (and the error message) is why a VLA cannot be initialized. That is answered here.

With a constant, array initialization works fine.

const char* list_of_words[20] = {"Some", "Array"};  // Good

Another issue is mistaking that const makes an object a constant. It does not.


Alternative C code

int main(){
    // unsigned int const size_of_list = 20;
    #define size_of_list 20u

    const char* list_of_words[size_of_list] = {"Some", "Array"};
    for (unsigned int writing_index = 0; writing_index < size_of_list; writing_index ++)
        ;
    return 0;
}

If you can specify the size in the array itself, then you can get it's size with the sizeof operator. This may be better suited for having the compiler count up the size instead manual counting. When not using C99 VLAs, sizeof also yields a compile-time constant.

#include <stddef.h> /* size_t */
int main(void) {
    const char* list_of_words[] = {"Some", "Array"};
    const char list_of_char[sizeof list_of_words
        / sizeof *list_of_words] = {'S','A'};
    const size_t size_of_list
        = sizeof list_of_words / sizeof *list_of_words;
    for (size_t writing_index = 0;
        writing_index < size_of_list; writing_index ++);
    return 0;
}
Neil
  • 1,767
  • 2
  • 16
  • 22
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256