is it possible to create array of pointers and set + get its elements using a class like this?. make changes/add to the class as needed. show actual code, if possible.

class t
{
t::t()
{
}
t::~t()
{
free(array);
array = NULL;
}
void set_size(int s)
{
  array = new int [s];
}
void set(int n, int x)
{
   array[n] = x;
}
int get(int n)
{
   return array[n];
}
int *array;
};

Recommended Answers

All 2 Replies

If you want array to store pointers, you'll have to make it an array of pointers:

array = new int* [s];

array will be pointer to an int pointer, like this:

int** array;

When you set the size in your code, if the size has already been set, you will be losing all the already stored information and you'll have a memory leak, so you'd best do something about that.

When you want to set an int* in the array, pass in the int*

void set(int n, int* x)
{
array[n] = x;
}

Similarly, when fetching an int*, make the return type int*

int* get(int n)

is it possible to create array of pointers and set + get its elements using a class like this?. make changes/add to the class as needed. show actual code, if possible.

class t
{
t::t()
{
}
t::~t()
{
free(array);
array = NULL;
}
void set_size(int s)
{
  array = new int [s];
}
void set(int n, int x)
{
   array[n] = x;
}
int get(int n)
{
   return array[n];
}
int *array;
};

This reeks of homework. Why not share with us your thoughts first?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.