Thread: quick question

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    quick question

    I want to have an array that holds the number of enemies in the game. Except I have no idea what the user is going to choose for that number..........I need this array to be global however beause it is needed over many functions.......how could I go about doing this??
    I am lost

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Sounds like you need dynamic memory not a global. You cannot make an array if you do not know its size as a compile time constant without dynamic memory.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Simply because the variable is dynamic does not mean that it cannot be global.

    Look up in your C++ book the 'new' keyword. Also, you should consider not making the variable global. This goes against the OO ideals and is simply bad style.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    This is how you code your requirement

    #include <iostream>

    int *ptr_to_no_of_enemies = 0;

    int main()
    {
    int no_of_enemies = 0;
    std::cout << "Enter the number of enemies : ";
    std::cin >> no_of_enemies;

    ptr_to_no_of_enemies = new int[no_of_enemies];
    /* you are dynamically creating an array and passing the starting
    address to a pointer which is global. Using this pointer, you
    can work with the contents of the array
    */

    // call all your functions out here.. and get your job done

    /*
    please ensure that you free the memory allocated to create the
    array dymanically
    */
    delete [] ptr_to_no_of_enemies;
    return 0;
    }

    PS: As mentioned by others, its preferable you don't create a global variable and allow that variable to point to a dynamically allocated array
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208

    thanx

    shiv_tech_quest for the example...............I wouldn't use global variables if I knew of another way to do it
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM