Peng Zhang
Peng Zhang

Reputation: 3585

Return a pointer to a newed array

A pointer to an array is declared as Type (*p)[N]; . For example,

int a[5] = { 1, 2, 3, 4, 5 };
int(*ptr_a)[5] = &a;
for (int i = 0; i < 5; ++i){
    cout << (*ptr_a)[i] << endl;
}

would ouptut the five integers in a.

How to convert from new int[5] to the type int (*p)[5].

For example, when I write a function that returns a pointer to a new array, the following code doesn't compile.

int (*f(int x))[5] {
    int *a = new int[5];
    return a; // Error: return value type does not match the function type.
}

It produces:

error: cannot convert ‘int*’ to ‘int (*)[5]’

Upvotes: 4

Views: 185

Answers (6)

Markus Mayer
Markus Mayer

Reputation: 544

C++11 have a better way of dealing with fixed sized arrays. I recommend to use std::array instead of c-style arrays. A modern compiler should emit equal efficent code, than the pointer version.

std::array<int,5> f()
{
    std::array<int,5> a;
    return a; 
}

If you really want to mess with pointers, use the following.

std::array<int,5>* f()
{
    std::array<int,5>* a = new std::array<int,5>;
    return a; 
}

I additionally recommend to not use raw pointers but smart pointers (eg. std::unique_ptr), to prevent memory leaks by forgetting to delete the array.

typedef std::array<int,5> array_of_5_ints;
std::unique_ptr<array_of_5_ints> f()
{
    std::unique_ptr<array_of_5_ints> a = new std::array<int,5>;
    return a; 
}

Upvotes: 1

haccks
haccks

Reputation: 106002

Change your function to

int (*f())[5] {
    int *a = new int[5];
    //code ...
    return (int (*)[5])a;
}

Upvotes: -2

Ruslan Gerasimov
Ruslan Gerasimov

Reputation: 1782

#include <iostream>
using namespace std;

int (*f(int x))[5]
{
    int (*a)[5] = new int[1][5];
    return a; // Error: return value type does not match the function type.
}


int main(void)
{

 int a[5] = { 5, 4, 3, 2, 1 };
 int(*ptr_a)[5] = &a;
 for (int i = 0; i < 5; ++i)
 {
    cout << (*ptr_a)[i] << endl;
    cout << f(i) << endl;
 }
}

Upvotes: 1

R Sahu
R Sahu

Reputation: 206567

You can use:

int (*a)[5] = new int[1][5];

Example:

#include <iostream>

int main()
{
   int (*a)[5] = new int[1][5];
   for ( int i = 0; i < 5; ++i )
   {
      (*a)[i] = 10*i;
      std::cout << (*a)[i] << std::endl;
   }
   delete [] a;
}

Output:

0
10
20
30
40

Upvotes: 5

Mark B
Mark B

Reputation: 96233

You have this tagged C++ so please just don't do this. Use vector instead. It cleans up the whole syntax and makes sure you don't leak memory!

std::vector<int> f(int x)
{
    std::vector<int> a(5);
    return a;
}

Upvotes: 1

Tony Delroy
Tony Delroy

Reputation: 106076

You can clean up your code using a typedef, then it's easier to see how to get it working:

#include <iostream>

typedef int (*P_array_of_5_ints)[5];

P_array_of_5_ints f() {
    int *a = new int[5];
    *a = 42;
    return (P_array_of_5_ints)a;
}

int main()
{
    P_array_of_5_ints p = f();
    std::cout << (*p)[0] << '\n';
}

(see it running here at ideone.com)

Upvotes: 4

Related Questions