Undefined Type or something of sort

This is a discussion on Undefined Type or something of sort within the C++ Programming forums, part of the General Programming Boards category; How would you got about doing something along the lines of: Code: int size; char array[size]; This gives an error ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    Undefined Type or something of sort

    How would you got about doing something along the lines of:

    Code:
    int size;
    char array[size];
    This gives an error because size is not set. Even if I initialize size at start up and then give it a value later on, array[size] will remain at the original value of size. I am trying to avoid declaring the char array after getting the info from the user. Dont ask me why but thats what I want to do. I believe it is possible but not sure. Thanks for any feed back.

    Bitphire

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ignoring compiler extensions, array sizes must be known at compile time. Their is no standard method to make the array size based off of user input.

    There are alternatives however:

    make array a char * and after you get the size from the user use new[] to allocate the memory. Just remember to use delete[] to deallocate it.

    You can also use a vector<char>. After you get the user's input call the resize member function to set the size.

    Since you are dealing with an array of char you can also look at std::string

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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