A function that takes an int arg and returns a pointer to an int[5] array

Hi

I am trying to create a simple function that takes an int arg and returns a pointer to an int[5] array.

The array and function definition are defined as follows and compile correctly.
However, the code to invoke the function and receive the returned array, won't compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   int ia[5] = {10, 20, 30, 40, 50};   /// array

   int (* f3a (int)) [5]               /// function
   {
      return &ia;
   }

   int main()
   {
      
      int* iar1 = f3a(1);

      return 0;
   }


This gives the following compilation error:

error: invalid conversion from 'int (*)[5]' to 'int*' [-fpermissive]|


To solve this problem, I need to define the function differently - to return an int pointer, and then the function code returns the array. Then, invocation is perfectly OK.

The question is : How can the first technique to define a function that returns an array, be made to work? It is based on an example given by Bruce Eckel in TICPP. Hence the query.


Thanks
Steven

The error says that the type returned by f3a is not int* (or anything that would implicitly convert to int*).

One can get around that error:
1
2
3
4
int main() {
  auto iar1 = f3a(1);
  return 0;
}

However, using iar1 might turn out to be difficult.


What are you trying to achieve (aka I have no idea what the TICPP is)?
Last edited on
One can get around that error:
int main() {
auto iar1 = f3a(1);
return 0;
}


Unfortunately, this never worked for me
Just like keskiverto, i also dont have any idea about TICPP.
But this is how it worked:
1
2
3
4
5
6
7
8
9
10
11
12
 int ia[5] = {10, 20, 30, 40, 50};   /// array

   int (* f3a (int)) [5]               /// function
   {
      return &ia;
   }

   int main()
   {
      int (* f3as ) [5]  = f3a(1);
      return 0;
   }

if you want to print the values of the array.

This works:
1
2
3
for(int i=0; i<5; i++){
        cout<<*(f3as[0]+i)<<endl;
      }



10
20
30
40
50
Last edited on
The auto requires C++11.

The *(f3as[0]+i) is same as f3as[0][i]

The int (*foo) [5] is a "pointer to array". It is just a pointer that returns a pointer, but it has special pointer math coefficient.

If bar is an int array of some size and foo points to its first element &(bar[0]) == &(foo[0][0]), then &(foo[x][y]) == &(bar[5*x+y])

The pointer to array syntax is rarely used. As you should notice from above, it allows using (via pointer) an 1D array like it were a 2D array.
Thanks for the replies. I shall study them in detail.

TICPP stands for "Thinking in C++", the free e-book written by Bruce Eckel, who is also famous for his book TIJ ("Thinking in Java"). Both e-books are freely downloadable from Bruce Eckel's website www.bruceeckel.com.

Steven
Hi

Yes, The following code is working and thanks very much for the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int ia[5] = {10, 20, 30, 40, 50};   /// array

int (* f3a (int)) [5]                      /// function
{
   return &ia;
}

int main()
{
    /// Invoke the function directly.
    int (*iar1) [5]  = f3a(1);	             /// WORKS PERFECTLY
    print(*iar1);

    return 0;
}



However, I have also tried defining a pointer to the above function. That works's fine, but when I try invoking the function through the pointer, the program crashes:

1
2
3
4
int (* (*fp3a) (int)) [5];                  /// function pointer

/// Invoke the function through the pointer.
int (*iar2) [5] = (*fp3a) (1);	    /// PROGRAM CRASHES AT THIS LINE 


Any solution?

Thanks
Steven
Are you dereferencing an uninitialized pointer?
Yes, you are quite right. Actually, I had forgotten to initialize the function pointer to the function's address, and was trying the dereference the pointer though it was uninitialized.

The corrected code works perfectly:

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
37
38
39
40
41
#include <iostream>
#include <exception>

int ia[5] = {10, 20, 30, 40, 50};   /// array

int (* f3a (int)) [5]                      /// function
{
   return &ia;
}

int (* (*fp3a) (int)) [5] = f3a;     /// function pointer

/// Helper function to print an array.
void print(int ia[])
{
    for(int i = 0; i < 5; i++)
        cout << ia[i] << " ";
    cout << endl;
}

/** Arrays to store Function return values
     Each variable is a pointer to an int[5] array.

     This is necessary since the function returns
     a pointer to an int[5] array.
*/
int (*iar1) [5];
int (*iar2) [5];

int main()
{
    /// Invoke the function directly.
    iar1  = f3a(1);
    print(*iar1);

    /// Invoke the function through the pointer.
    iar2  = (*fp3a)(1);
    print(*iar2);

    return 0;
}



Thanks.
Steven
Topic archived. No new replies allowed.