Return array of pointer

Jan 17, 2015 at 9:29am
Hi,
I have array of struct.
I want return pointer of multiple from elements of array.
For example:
I have twenty element in array and each element is struct.
I search in array and find four element.
Now i want return pointer of four element.
Can you give me simple example?
Last edited on Jan 17, 2015 at 9:53am
Jan 17, 2015 at 10:14am
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
// Example program
#include <iostream>
#include <string>

struct test
{
    int a = 15;
    bool b = true;
    float z = 1.523745789;
};


int main()
{
    test s1,s2,s3,s4,s5;

    test array[5] = {s1,s2,s3,s4,s5};
    test *pElement1 = array+0;
    test *pElement2 = array+1;
    test *pElement3 = array+2;
    test *pElement4 = array+3;

   int testint =  (*pElement2).a;

    std::cout << testint;

    return 0;
}


an array = a pointer to the first element, ....
Jan 17, 2015 at 10:28am
Thanks you,But i search in twenty element of array and find four or five element.
Now return pointer of these elements from function.
Jan 17, 2015 at 10:33am
closed account (SECMoG1T)
Hi am sure that would be quite exhausting ,please note that a function can't return a static array because such arrays can't be copied, however a pointer or reference to an array can be returned but even though thet possible such a function would have a very complex syntax for 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
struct Foo
 {
  ///code here
 }

 Foo*(*) Search (Foo& bar) [size]
 {
   ///returns a pointer to an arry of pointers
 }

///a better way
 auto Search (Foo& bar)-> Foo*(*)[size]
 {
  ///second approach
 }

///3rd
 Foo* (ptr)[size]//ptr is a pointer to an array of size Foo*
 
decltype (ptr) Search (Foo& bar)
 {///
 }

///4th
using type=Foo*(*)[size]

type Search (Foo& bar)
 {
 ////
 }
.
.
.
.
.


Am sure you will like neither of them, rather i'll recommend that if you have to return multiple values you should consider returning a vector containing copies or pointers to the elements.

That will be easier indeed.
Last edited on Jan 17, 2015 at 11:02am
Jan 17, 2015 at 11:33am
Indeed. Use standard containers, like vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<Foo*> findAddresses( Foo[] array, size_t n, Bar key );

int main() {
  const size_t N = ...
  Foo arr[N]; // array of struct
  Bar value;
  // set arr and value here

  auto match = findAddresses( arr, N, value );

  // Show found Foos
  for ( auto x : match ) {
    std::cout << *x << '\n';
  }
  return 0;
}
Jan 17, 2015 at 1:20pm
I hope you find this useful ...

(only standard arrays & pointers)

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>
#include <string>

struct test
{
    int a = 15;
    bool b = true;
    float z = 1.523745789;
};

test ** function()
{
    test s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20;
    s2.a = 4;
    test array[20] = {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20};
    test * (pArray[20])={};
    for (int i=0;i<20;i++)
        *(pArray+i) =  (array +i);
   return pArray;

}
int main()
{
     
     test ** pArray = function();
     
     //now testing if array was returned correctly
     int testint = (*(*pArray+0)).a;
     int testint2 = (*(*pArray+1)).a;
     test test2 =  *(*pArray+1);
     std::cout << testint <<"  "<< testint2 <<"  " <<test2.a;

     return 0;
}


I know it's not the full solution. I Return an array with all the pointers

So I suppose you have to make the pointer array dynamic,
and send an array with the array numbers of the 4 items you are looking for.
*(pArray+i) = (array +i);

iterate that array and use the number for I
now the function will return an array with the pointers to the found elements.

Last edited on Jan 17, 2015 at 1:34pm
Jan 17, 2015 at 2:49pm
skorefish's solution produces undefined behavior because it returns a pointer to a local variable.

Rather than returning the array, which may incur the cost of copying the array from the return value to its final destination, pass the array into the function as a parameter:
void func(int input[], int *output[4]);
How many items can you return? In this example, it's fixed at exactly 4. If your code can return a variable number of elements then a vector<> would be more appropriate:
void func(int input[], std::vector<int*>output);
Jan 17, 2015 at 3:13pm
If the output vector is a parameter, then it has to be a reference parameter.
void func( int input[], std::vector<int*> & output );
Jan 18, 2015 at 3:41pm
If the output vector is a parameter, then it has to be a reference parameter.

Doh! Thanks for the correction.
Topic archived. No new replies allowed.