Thread: array in structure help

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    array in structure help

    I have a structure with an array, but the array size I know only after my program has done something else which would then set the varSize variable. But how do I do this if the array is in a structure??

    Code:
    struct database
    {
      int anArray[varSize]
    };
    how do I "pass" in varSize so when I make an instance of the structure it creates an array with "varSize" number of elements? I don't wnat to use global variables to keep in practise of good programming

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You'll need to use the new operator to allocate memory at runtime. Otherwise, your array size must be a constant known at compile time.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    To expand on joshdick's statement, here's some code:

    Code:
    struct database
    {
       int *anArray;
    };
    
    .
    .
    .
    
    int main()
    {
          int size;
    
          cin >> size;
    
          database db;
          db.anArray=new int[size];
    
          //Use the array however you want
    
         delete [] db.anArray;
         return 0;
    }
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Thanks joshdick and bennyandthejets

    I guess new allocattes memory on the heap and delete frees up resources after

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by bobnet
    Thanks joshdick and bennyandthejets

    I guess new allocattes memory on the heap and delete frees up resources after
    The Force is strong with this one.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure or Array?
    By epi_jimenez in forum C Programming
    Replies: 7
    Last Post: 04-01-2009, 02:45 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM