0

In a book (C++ Primer) I found that "A nonconst variable or a const variable whose value is not known until run time, cannot be used to specify the dimension of an array". Then I made 2 programs to test it:

     `
        #include <iostream>
        using namespace std;
        //PROGRAM 1
        int main() {
        const int j;
        int k[j];
        return 0;
        }

program one was bound to fail as j hasn't been initialized despite being constant.

   `
   int main()   //PROGRAM 2
    {
    int k;
    cin>>k;
    int p[k];
    return 0;
    }

This works fine but it contradicts the book. Please tell me if that book statement was for some version other than C99.

  • The book is about C++, not C. – juanchopanza Apr 22 '17 at 10:47
  • @juanchopanza It is, but the answer to the question (which you have referenced while marking this as duplicate) mentions C99. –  Apr 22 '17 at 10:51
  • Yes it does. So what? The book you mention is talking about C++. So it doesn't make sense to ask if it talks about some version of C. – juanchopanza Apr 22 '17 at 10:58
  • @juanchopanza the question you mentioned also talks the same about c version & c++ –  Apr 22 '17 at 11:13
  • @infinite - So here is another duplicate, perhaps closer to your question: [In C++ books, array bound must be constant expression, but why the following code works?](http://stackoverflow.com/questions/5947661/in-c-books-array-bound-must-be-constant-expression-but-why-the-following-cod) – Bo Persson Apr 22 '17 at 11:43

0 Answers0