Return pointer as array

trying to return an array as a pointer

Page 1 of 1

9 Replies - 1519 Views - Last Post: 08 June 2010 - 11:27 AM Rate Topic: -----

#1 Guest_hudrat*


Reputation:

Return pointer as array

Posted 07 June 2010 - 09:38 AM

I want to create a function that returns an array of integers with a length of 36. As far as I'm aware I cannot return int[36] but have to pass as a pointer but I dont know what I need to do to make it compile?.

int* CreateSequence(char c)
{
	int* r;
	int sequence[36];
	r = sequence;

	if (c == 'a')
		sequence = { 0,1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1 };
	else
		sequence = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
	return r;
}



Is This A Good Question/Topic? 2

Replies To: Return pointer as array

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Return pointer as array

Posted 07 June 2010 - 09:49 AM

Just allocate some memory and return a pointer to that array
for example

#include <iostream>

using namespace std;

int *returnIntArray()
{
	// Should deallocate this memory with delete [] arrayInt
	int *arrayInt = new int [2];
	arrayInt[0] = 10;
	arrayInt[1] = 223;
	return arrayInt;
}

int main()
{
	int *ptrInt = returnIntArray();
	
	cout << ptrInt[0] << " " << ptrInt[1];
	cin.get();
	return 0;
}


WOuldnt it be best to use booleans though, as you are only storing ones and zeros?

This post has been edited by Aphex19: 07 June 2010 - 10:00 AM

Was This Post Helpful? -1
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Return pointer as array

Posted 07 June 2010 - 09:51 AM

You just accessed your array out of bounds.
There are two elements in your array.
0 and 1.

Quote

 int *arrayInt = new int [2];
        arrayInt[0] = 10;
        arrayInt[1] = 223;
        arrayInt[2] = 456;


However, the dynamic allocation is necessary because your array's memory is deleted when the function returns!!

Look up dynamic memory allocation, the operators "new" and "delete" .

This post has been edited by taylorc8: 07 June 2010 - 09:54 AM

Was This Post Helpful? 1
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Return pointer as array

Posted 07 June 2010 - 09:54 AM

All you can return is a pointer. Whether or not it points to the base of an array is up to you. As params and return values, C++ inherited C's "arrays are treated as pointers" quirk.
Was This Post Helpful? 1
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Return pointer as array

Posted 07 June 2010 - 09:56 AM

View Posttaylorc8, on 07 June 2010 - 08:51 AM, said:

You just accessed your array out of bounds.
There are two elements in your array.
0 and 1.

Quote

 int *arrayInt = new int [2];
        arrayInt[0] = 10;
        arrayInt[1] = 223;
        arrayInt[2] = 456;


However, the dynamic allocation is necessary because your array's memory is deleted when the function returns!!

Look up dynamic memory allocation, the operators "new" and "delete" .


Yeah, i agree, sorry, I was accessing out of bounds memory. EDIT: Memory was deleted when the function returns, got it ;)

This post has been edited by Aphex19: 07 June 2010 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#6 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: Return pointer as array

Posted 07 June 2010 - 02:18 PM

You do have a couple of options other than returning int*.

You could define a fixed size array or array pointer as a type and return that, ensuring that you are passing round the correct sized array

static const int size = 36;

typedef int (*Array36Ptr)[size];

static Array36Ptr CreateSequence();

int main()
{
    Array36Ptr may = CreateSequence();

    delete[] may;

    return 0;
}

Array36Ptr CreateSequence()
{
    Array36Ptr result = new int[1][size];

    if (result != NULL)
    {
        for(int ix = 0; ix < size; ix++)
        {
            (*result)[ix] = size - ix;
        }
    }

    return result;
}



Although the syntax in the function is a little odd. Or of course you could just use a vector which since it is an object can be returned without any problem.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Return pointer as array

Posted 07 June 2010 - 02:48 PM

if the dynamic allocation does not make sense to you, you can also declare the array as static which means the array will never go out of scope through the entire course of the program (that is if you keep you handle to it)
Was This Post Helpful? 0
  • +
  • -

#8 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: Return pointer as array

Posted 07 June 2010 - 03:36 PM

While having static data in the function for it to return is a possibility the OP needs to be aware of the limitations this imposes. Specifically any function using static data like that tends not to be re-entrant which can be an issue in a multi-threaded environment (in a program using more than one thread).
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Return pointer as array

Posted 08 June 2010 - 08:31 AM

There are many reasons why C++ programmers avoid arrays, and the fact that you can't assign arrays is one. If you could do something like array1 = array2 in C/C++ than it would make sense to be able to write functions that return arrays... but this is not allowed. In C++ one would normally use vectors (or another container class) over raw arrays -- there are MANY reasons for this, least of all is the fact that you can use these as return values from functions.

In C I often used the work around of wrapping my array in a struct. You CAN return a struct even if it contains an array:
typedef struct { int array[36]; } IntArray36Wrapper;
This kind of solves the problem -- but in the end it is just better to pass by reference and avoid the whole wrapper mess.

In C++ you can make a "fancy" wrapper that looks and acts like an array but isn't. You do this done by judiciously overloading operators. (well I think you will always run into some "rough edges" where your wrapper and the built in array type diverge in behavior).

Normally though the way to return an array from a function is to pass by reference:

void CreateSequence(int*& array, char arg);

OR to ditch those buggy arrays and work with a C++ collection:

vector<int>& CreateSequence(char arg);
Was This Post Helpful? 0
  • +
  • -

#10 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Return pointer as array

Posted 08 June 2010 - 11:27 AM

Also, I'm not sure if your array needs to contain anything other than 1 or 0, but a vector<bool> will optimize for storage efficiency, only one bit is really required for a bool.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1