Thread: array user definable size

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    array user definable size

    I want to make a user definable size array..
    can it be done?
    I tried this so far but i get 3 errors..

    Code:
    cout << "Enter the size of the Array: ";
    unsigned int size;
    cin >> size;
    if(size == 0)
    {
    	cout << "Wrong Value!";
    	return 0;
    }
    int array[2][size];
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try this:

    cout << "Enter the size of the Array: ";
    unsigned int size;
    cin >> size;
    if(size == 0)
    {
    cout << "Wrong Value!";
    return 0;
    }

    char * myArray;
    myArray = new char[size];

    It's called dynamic memory allocation. For all practical purposes myArray is now an array of char of capacity size. Whenever you allocate memory dynamically you should always release it as well. For arrays you would do:

    delete [] myArray;

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or use an std::vector, calling the method resize().
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    thx but i really need an int array..

    Im making a function to invert twho-dimension int array

    so i wanted to add user definable array so i could verify it works with evry array size..

    anyways I guess ill change my array manually...
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM