Declaring Pointer Array in Class

Hey everyone!

I have 5 addresses like this:
0x8017b0
0x8017c0
0x8017d0
0x8017e0
0x8017f0

And have to place them in an array. How can I declare an array in class, place these addresses, and reach them whenever I want?
Last edited on
What are you actually trying to solve by doing this?

Making an array of pointers in itself is not hard. Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Example program
#include <iostream>

class Data {
  public:
    Data()
    {
        for (int i = 0; i < 5; i++)
        {
            data[i] = 13 * (i*i); // some number
            pointers[i] = &data[i];
        }

    }
    
    int* pointers[5]; // the array of int pointers
    
  private:
  
    int data[5]; // the actual data we're referencing through the pointers. You can do it your own way to what suits your needs.
};

int main()
{   
    Data data;
    
    for (int i = 0; i < 5; i++)
    {
        std::cout << "Data at address " << reinterpret_cast<void*>(data.pointers[i]) << " == " << *data.pointers[i] << std::endl;
    }

    int a = 42;
    data.pointers[2] = &a;

}
Last edited on
Thank you for example codes. Actually I am trying to read data from text file (which is not known length) and keep them in heap memory.

What are you actually trying to solve by doing this?
Thats actually homework.

Text be like this: 1 2 3 4 5 6

Basicly I am trying to keep this outputs addresses and reach from main. But array will be another class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class Age {
public:
	Age(int input)
	{
		m_Age = input;
	}
private:
	int m_Age;
};

int main()
{
	for (int i = 0; i < 50; i+=5)
	{
		Age *a = new Age(i);
		std::cout << a << std::endl;
	}
	std::cin.get();
	return 0;
}


00067BD8
00065B88
00065BB8
000658B0
000658E0
00065910
000659F0
00065A20
00065A50
0006FF70
Last edited on
Actually I am trying to read data from text file (which is not known length) and keep them in heap memory.


You really should consider std::vector instead of all the manually allocated memory with new.

Also with your small example there is no reason for all the pointers, just store the actual value in the vector/array.

Topic archived. No new replies allowed.