Home > Software design >  Creating t* array with adresses to t array fields
Creating t* array with adresses to t array fields

Time:12-08

Can the following code be shortened?

size_t n_arr{4};
int arr[4]{1, 4, 5, 6};
int* arr_p[4];

for (; n_arr—; )
   arr_p[i] = &arr[i];

The adresses are all chained together, so is there a more efficient way of grabbing a block of adresses and storing them in another array?

CodePudding user response:

I think you could write this:

size_t n_arr{4};
int arr[4]{1, 4, 5, 6};
int* arr_p[4];
int **arr_pp;

arr_pp = arr_p;
for (; n_arr—; )
   *arr_pp   = arr  ;

But as Yksisarvinen wrote, having an array of sequential pointers does not seem very useful.

CodePudding user response:

size_t n_arr{4};

int* arr_p[4]=&n_arr[4]{1, 4, 5, 6};

I believe this would work. not completely sure. :D

  • Related