Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Question about const and arrays in C++

Options
  • 20-10-2006 10:22pm
    #1
    Registered Users Posts: 3,803 ✭✭✭


    Hi all,

    I have been doing c++ for about a month now so the following question will probably be a basic one. Basically i have an array of bombs and i want the user to choose how many bombs to make (eg. the size of the bomb array).
    This is what i want to do.
    const int num;

    cout << "Enter number of bombs" << endl;
    cin << num;

    BOMBS bomb[num]; // i have created a sturcture of type BOMBS

    Of course this won't work. From what i gather, the size of the bomb array has to be a const value. From what i hear, i think a variable of type const has to be intialized and declared at the same time, and cannot be changed later on.

    How do i do the above?

    Thanks very much in advance.


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    In this type of scenario in C what you'd have to do is called malloc and dynamically allocate the number of bytes needed.

    i.e. bomb = malloc(sizeof(bomb)*num);
    (or something like that)

    Do you have to do something like that in C++?


  • Registered Users Posts: 95 ✭✭Mr.StRiPe


    BOMBS *bomb = new BOMBS[num];

    This will do the job.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    or why not keep a variable to keep track of the size:
    int num;
    BOMBS bombsarray[100];
    cout << "Enter number of bombs: ";
    cin >> num;

    then use num in your for loops or what ever instead of the capacity of the array 100. Of course the capacity should always be the maximum bombs you want


  • Registered Users Posts: 3,803 ✭✭✭Benzino


    In this type of scenario in C what you'd have to do is called malloc and dynamically allocate the number of bytes needed.

    i.e. bomb = malloc(sizeof(bomb)*num);
    (or something like that)

    Do you have to do something like that in C++?
    Thanks for the reply. I have never done C, only java and now starting C++. so i'm not aware of the maloc method, thanks though for your help.
    BOMBS *bomb = new BOMBS[num];

    This will do the job.
    Oh, use pointers, never thought of that. I have only learned about them this week (Lliterally in our last c++ class on thursday!!!) but i didn't know you could do it for structures.

    So are you creating a pointer bomb of type BOMBS to a new array, allowing the size of the bomb array to change? I just want to be able to understand what it is doing. Thanks for your help.
    or why not keep a variable to keep track of the size:
    int num;
    BOMBS bombsarray[100];
    cout << "Enter number of bombs: ";
    cin >> num;

    then use num in your for loops or what ever instead of the capacity of the array 100. Of course the capacity should always be the maximum bombs you want
    Hey thanks for the reply.I had it like that orignally, but if the user did enter 101 for example, then the program would crash. But the main reason i want to change it is so that it'll be efficient. The user might decide to only create 5 bombs, while memory has be allocated for 100, although in a small program like this it won't matter.

    Thanks again for all the help.


  • Registered Users Posts: 95 ✭✭Mr.StRiPe


    So are you creating a pointer bomb of type BOMBS to a new array, allowing the size of the bomb array to change? I just want to be able to understand what it is doing. Thanks for your help.


    The new operator is like malloc(sizeof(bomb)*num) it dynamically allocates memory, when your programme is running, which will be used to store your BOMBS structures. It then returns a pointer to this new memory location so you can access them.

    BOMBS *bomb = new BOMBS[x] allocates enough memory for x BOMBS structures and passes back a pointer to the memory location of the first one.

    so *bomb can now be used to access your new BOMBS array.

    To access the bombs is the same as any array i.e. bomb[0] ... bomb[x]


  • Advertisement
  • Registered Users Posts: 3,803 ✭✭✭Benzino


    Oh i see, thanks very much for the help.


Advertisement