Thread: create an array of structures

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    20

    create an array of structures

    is it possable to do something like this?

    Code:
    struct program
    {
    	bla bla
    };
    
    int main()
    {
    int K = 6;
    
    program LocalMachine[K];
    Just need to create an array of the structure that will be different depending on what the variable K is equal to

    can't just put 6 because the value will be different every time the prog is run.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    program *LocalMachine = new program[K];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Prefer using vector for dynamic arrays:
    Code:
    std::vector<program> LocalMachine(K);

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    COOL

    thanks!

    salem.. used yours because I saw it before daved's.. how does that work exactly?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    20
    hmm.. never used vectors before..

    would I need to do the std::vector or would vector work? (just wondering for future stuff)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Salem's version is called a dynamic array because you allocate space for the number of items dynamically at runtime with new. You use it pretty much the same way you would use your static array (if K was const). When you are done with a dynamic array, you must free the memory with a call to delete:
    Code:
    delete [] LocalMachine;
    The vector is the C++ version of dynamic arrays. It is a class in the standard library that handles memory management for you, and is more flexible. You just have to look up vector in your reference book/tutorial and learn a few of its methods.

    Like with cout and cin, you must put std:: unless you have a using directive in your code. Assuming you are using <iostream>, if you don't need to put std::cout, then you don't need std::vector (vector will work).
    Last edited by Daved; 10-02-2005 at 09:52 AM.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Quote Originally Posted by Sephiroth222
    hmm.. never used vectors before..

    would I need to do the std::vector or would vector work? (just wondering for future stuff)
    if you are using the standard namespace thne you don't need to put std:: in front of vector.

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
    	vector<int> myVect; // use your defined type instead of 'int'
    
    	// Rest of program
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 01:40 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM