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;
};

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?

This article has been dead for over six months. Start a new discussion instead.