February 06, 2006

Const int or int for Array??

Do we have to use const int when assigning an array?? Below programme shows you that it is not a must!!!But, why in the past, we need to do that?? when C++ change the rule??? who change it actually?? Anybody knows about that??

#include

#include

using namespace std;

int main () {

int n,m, kk;

cout << "Pls input the initial size of a array:\t"<

cin >> n;

kk= 0; // for assign elements of arrays

for (int k=1 ; k<=5 ; k++) {

n +=1 ;

double U[n];

cout<

for (int j=0 ; j

kk+=1;

U[j]=kk;

cout<< "U["<

}

}

system ("pause");

return 0 ;
}


- 4 comments by 1 or more people Not publicly viewable

  1. Not sure what compiler you are using, but I think this is not standard C++. I checked Stroustrup and he says the array size must be a constant, and I tried this program in Visual C++

    #include<iostream>
    using namespace std;

    void main()
    {

    int n;
    cout << "Input n:\t";
    cin >> n;
    int U[n];
    }

    and got the following errors.

    c:\documents and settings\dan goodman\desktop\test.cpp(9) : error C2057: expected constant expression
    c:\documents and settings\dan goodman\desktop\test.cpp(9) : error C2466: cannot allocate an array of constant size 0
    c:\documents and settings\dan goodman\desktop\test.cpp(9) : error C2133: 'U' : unknown size

    So if your code really works for you, it's a bonus feature of your compiler.

    06 Feb 2006, 14:26

  2. I use Dev C++. Do you want to know more about this topic? go to this forum and Cirus give me a very good explanation !!

    link

    By the way, who is Stroustrup?? :P

    06 Feb 2006, 15:30

  3. Stroustrup invented C++, and I was referring to his book "The C++ Programming Language". I'm not sure that everything that Cirus told you is correct. My understanding is that space for storage of variables is allocated on the stack and not in the module unless it's a static variable.

    Anyway, what you "should" do, to make it proper C++ is to write:

    int *U = new int [n];
    check that U is not a null pointer (which happens if you have run out of memory)
    blah blah blah
    delete [] U;

    Alternatively, use the vector class from the STL.

    06 Feb 2006, 16:37

  4. Lovely!!!

    Thank you very much for your reply. :)

    06 Feb 2006, 22:49


Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

February 2006

Mo Tu We Th Fr Sa Su
Jan |  Today  | Mar
      1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28               

Search this blog

Galleries

Most recent comments

  • Thanks a lot for your info. I am puzzled because I cannot see anything about the GRUB during the ins… by on this entry
  • Well when I installed Fedora it will go through a stage where it should configure grub for you. GRUB… by on this entry
  • I can't unfortunately. I thought it was an expressionistic flourish (they characters come out as ran… by on this entry
  • Thank you, James. I will write more in the future. By the way, can you see the chinese poems above? by on this entry
  • This blog is awesome, I'm adding it to my favourites. by on this entry

Blog archive

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXXII