Thread: Stupid Q regarding array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    Stupid Q regarding array

    Hi,tried to define the size of an array by doing this:
    Code:
            int x=2;
    	int y=3;
    	int z=x*y;
    	int someArray[z];
    and I keep getting these errors:
    Code:
    error C2057: expected constant expression
    
    error C2466: cannot allocate an array of constant size 0
    
    error C2133: 'someArray' : unknown size
    In the programme itself x and y would be defined by user input and the size of someArray could be anywhere from 1 to 80.All errors go when i set someArray[80].

    Im more curious as to why it doesnt work than anything else,

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Standard C++ does not support variable length arrays. Use std::vector<int> instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Thanks Laserlight.

    Strangely enough bought accelerated C++ and was going to make a start on it this evening and one of the first subjects is the vectors

    Regards Cathalo

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's probably because std::vector would be your default choice for storing variable amounts of stuff in.

    Note, however, that if you used the const keyword you could declare your array as well. (But only if you are dealing with real constants whose value is known to the compiler. You can't for example get input at runtime and then create an array of that size, even if you store the entered value in a const variable.)

    Code:
    int main()
    {
        const int x=2;
        const int y=3;
        const int z=x*y;
        int someArray[z];
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    I get you Anon.So once the compiler sees that any of the variables can change and as a result the someArray could be of any size it throws up the errors.

    Normally I always knew what size the array should be so always declared haow many elements it had.

    I ended up trying the code I posted because I thought it was waste of memory and what have you setting up an array with 800 elements and only using the 1st 40 or so elements.

    And so thats what vectors are for

    Thanks for the replys and explanations

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Cathalo View Post
    I get you Anon.So once the compiler sees that any of the variables can change and as a result the someArray could be of any size it throws up the errors.

    Normally I always knew what size the array should be so always declared haow many elements it had.

    I ended up trying the code I posted because I thought it was waste of memory and what have you setting up an array with 800 elements and only using the 1st 40 or so elements.

    And so thats what vectors are for

    Thanks for the replys and explanations
    You're right. It would be wastage of memory if only a few elements are used. That's why dynamic allocation of memory is used and in C++ as you said vectors are used for the same purpose.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM