help with declaring array in which the user specifies size

This is a discussion on help with declaring array in which the user specifies size within the C++ Programming forums, part of the General Programming Boards category; here's what i want to do: program asks user how big the array is gonna be. cout << "how big ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Unhappy help with declaring array in which the user specifies size

    here's what i want to do:

    program asks user how big the array is gonna be.

    cout << "how big you want this array to be?" << endl;

    cin >> array_size;

    user types: 10(or however large he wants the array to be)[enter]

    program makes an array of size n:

    int array[array_size];

    BUT! alas......"error C2057: expected constant expression"

    the error occurs on this line: int array[array_size];


    i know theres a way to do this. and i also know this isnt the way....but its been a long time since i made progs...so someone please refresh my atrophied memory?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    cin >> array_size;
    int *array = new int[array_size];

    Then you do stuff like
    for ( int i = 0 ; i < array_size ; i++ ) array[i] = 0;

    When you're done, tidy up with
    delete [] array;

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    2
    thanks, salem

    but i dont know what exactly new or delete does....i feel like im gonna have trouble implementing those things without knowing what they do...and i saw that pointer * whats it for...its been too long...sorry

    can you help elaborate on what that code does exactly?

    appreciate it lots.

    edit: YAY! i just found a tutorial on dynamic memory! i think its gonna answer some questions.
    Last edited by ibanezrgking; 02-10-2002 at 02:31 AM.

  4. #4
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Well, as an array is essentially a pointer to a contiguous memory block, we can delcare an int pointer instead of an array. The new operator will return the address of a block of memory the size of what you wish to allocate, therefore your pointer becomes an array of whatever size you wish! As this was allocated dynamically, the program has no record of it so it's up to you to free up the memory with the delete operator, or else you'll get leaky code.
    MSVC++ 6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Array Help Please (user defined size)
    By Planetx33 in forum C++ Programming
    Replies: 9
    Last Post: 04-07-2007, 04:36 PM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Replies: 42
    Last Post: 12-19-2004, 07:59 AM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21