How to define a pointer array of pointers and how to access it?

I’m writing a game that needs 5 enemies. I have a class name “enemy” and here’s how I define the enemy pointer.

enemy* enemy1;
enemy* enemy2;
enemy* enemy3;
enemy* enemy4;
enemy* enemy5;
enemy1 = new enemy(enemypic[0], 100, 400, 494, 454,0.1,0.1, 10, attack[0], blood[0]);

Here’s how I build the array of pointers of the enemies and how I access them:

enemy* enemies[]={
    enemy1,enemy2,enemy3,enemy4,enemy5
};
for (int i = 0; i < 5; i++) {
    cout << i << endl;
    cout << enemy1 << endl;
    cout << enemies[i] << endl;
    if (!enemies[i]) {
        enemies[i] = new enemy(enemypic[i], 100, 400, 494, 454, 0.1, 0.1, 10, attack[0], blood[0]);
    }
}

But the position of enemy1 and enemies[0] is not the same, why is this happening and how should I write to make the position the same?

  • 4

    why do you want pointers?

    – 

  • if you need help with non-working code you should include a minimal reproducible example, expected and actual output

    – 

  • 2

    Just use modern C++ STL containers.

    – 

  • 5

    std::vector<enemy> enemies({ {...}, {...}, {...}, {...} });??? Avoids pointers and all those local variables and scales well (if you want to add further enemies); alternatively if numbers of enemies always remains fixed (for as long a as the programme runs) std::array<enemy, NumberOfEnemies> enemies( { ... } );

    – 




  • 1

    In current C++ try to avoid naked new/delete, first try to make a solution using any of the STL containers, std::vector, std::list, std::array, std::string, std::map etc. Where are you learning C++ from, it seems a bit of an outdated source.

    – 




The positions of enemy1 and enemies[0] are not the same because when you access enemies[0], it’s a separate pointer that points to the memory location where the value of enemy1 is stored. They are distinct variables even though they are both pointers to the same type.

If you want enemies[0] to point to the same object as enemy1, you should directly assign enemy1 to enemies[0]:

enemies[0] = enemy1;

Here’s the modified part of your code:

// Define pointers
enemy* enemy1;
enemy* enemy2;
enemy* enemy3;
enemy* enemy4;
enemy* enemy5;

// Assign initial values
enemy1 = new enemy(0, 100, 400, 494, 454, 0.1, 0.1, 10, 0, 0);
enemy2 = nullptr; // Initialize to nullptr
enemy3 = nullptr;
enemy4 = nullptr;
enemy5 = nullptr;

// Build the array of pointers
enemy* enemies[] = { enemy1, enemy2, enemy3, enemy4, enemy5 };

// Access and initialize if necessary
for (int i = 0; i < 5; i++) {
    std::cout << i << std::endl;
    std::cout << enemies[i] << std::endl;
    if (!enemies[i]) {
        enemies[i] = new enemy(i, 100, 400, 494, 454, 0.1, 0.1, 10, 0, 0);
    }
}

// Now enemies[0] and enemy1 point to the same object

// Cleanup (don't forget to delete allocated memory)
for (int i = 0; i < 5; i++) {
    delete enemies[i];
}

This way, changes made to the object through enemy1 will also be reflected when accessing the object through enemies[0], as they both point to the same memory location.

As already said in the comments above, don’t use pointers or new/delete in new programs, too error prone and outdated (old style – C like) programming.

Instead use STL containers, e.g.

class enemy {
public:
    //needed for test in the iteration below
    operator bool() const noexcept { return m_initialized; }
private:
    bool m_initialized{false};
};

using enemy_list = std::vector<enemy>; //meaningful alias
enemy_list enemies{5}; //reserve 5 uninitialized enemies

for (enemy& e : enemies) { //iterate over all enemies in the list
    if (!e) { //the bool operator is needed here
        e = enemy(some, arguments); //construct a new enemy
    }
}

Allocated memory will be freed automatically, no dangling pointers, safe and sound.

Leave a Comment