Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 29th, 2006, 11:45 PM   #1
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Function array

I've hit a brick wall when trying to get a function to return an int array. Here's what my code looks like:
//prototype
int GetInvent();

//definition
int Player::GetInvent()
{
    for(int i = 0; i < inventory; i++)
    {
        return inventorymax[i];
    }
}
I have tried these two methods, but no luck either:
//prototype
int[] GetInvent();

//definition
int[] Player::GetInvent()
{
    for(int i = 0; i < inventory; i++)
    {
        return inventorymax[i];
    }
}


// And

//prototype
int GetInvent()[];

//definition
int Player::GetInvent() []
{
    for(int i = 0; i < inventory; i++)
    {
        return inventorymax[i];
    }
}

I didn't think my second solution would work, but niether of them did so, any help?
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Mar 30th, 2006, 12:18 AM   #2
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
I see a couple problems with this (first solution).. first of all are you trying to return the whole array, or are you trying to return a position in your array? if your trying to return the whole array, you need to have the return time of vector<int>, instead if just int. and another thing that seemed a bit off was your for loop; you have it returning a position in the array... but it would only loop through once and hit the return an exit this fucntion. try this if your trying to return the whole array... i am assuming your array is a vector, if not you will get the general idea...

//definition
std::vector<int> Player::GetInvent()
{
  return inventorymax;
}
maybe be a bit more specific if thats not what your looking for
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Mar 30th, 2006, 3:47 AM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
I really don't understand what you want to do the way you put it. Maybe you will need to provide some more code, so that we can see what is 'inventorymax'.

But basically, regardless wether returning a whole array is possible, you can set your functions as 'void' and modify the functions so that they can work with pointers. That way, when you call the functions, the arrays that you have given as arguments will be changed. No need to return anything that way.

EDIT: I think that I have found something that may help you:
check this out.

Seems you cannot return a whole array. Look at this in the page I gave you:
#include <iostream>

#define LENGTH 5

using std::cout;
using std::endl;
using std::cin;

int* getArray();
void printArray(int* array);

int main(){
	int *p;
	cout << p;
	return 0;
}

int* getArray()
{
	int *array = new int[LENGTH];
	for ( int i = 0; i < LENGTH; i++ )
	{
		cout << "Enter an integer: ";
		cin >> array[i];
	}
	cout << "array in getArray()" << endl;
	printArray(array);
	return array;
}

void printArray(int* array)
{
	for ( int i = 0; i < LENGTH; i++ )
		cout << *(array+i) << " ";
	cout << endl;
	cout << endl;
}
The function getArray() seems to return an array, but it actually returns the reference to that array, so that it can be assigned to the 'pointered' to the new array in main(). I believe this is a very good example of such a problem.
__________________
Project::Soulstorm (personal homepage)

Last edited by Soulstorm; Mar 30th, 2006 at 4:00 AM.
Soulstorm is offline   Reply With Quote
Old Mar 30th, 2006, 4:27 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Within a class it is something like:
#include <iostream>

using namespace std;

class Player
{
	private:
		int inventorymax[10];
		int inventory;
	public:
		Player();
		void SetInventory(int inv);
		int* GetInvent();
		int GetInventItem();
};

Player::Player()
{
	inventorymax[0] = 3;
	inventorymax[1] = 2;
	inventorymax[2] = 1;
}

void Player::SetInventory(int inv)
{
	inventory = inv;
}

int* Player::GetInvent()
{
	return inventorymax;
}

int Player::GetInventItem()
{
	static int i = 0;
	//for(; i < inventory;)
	while(i < inventory)
	{
		return inventorymax[i++];
	}
	return 0;
}

int main()
{
	int inv = 3;
	Player player1;

	player1.SetInventory(inv);

	int *array = player1.GetInvent();

	cout << array[0] << array[1] << array[2] << endl;

	for(int i = 0; i < inv; i++)
		cout << player1.GetInventItem() << endl;

	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; Mar 30th, 2006 at 4:41 AM.
nnxion is offline   Reply With Quote
Old Mar 30th, 2006, 5:09 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
The closest things you can get to returning an array follow. I assume for sake of discussion you want to return an array of int.

1) Return a pointer to a statically allocated array;
int *Array()
{
    static int array[5];
    return array;
}
The problem with this is that the array is static. Which means you can't obtain two arrays from it (two calls will both return the same pointer). The length must also be known at compile time.

2) Return a pointer to a dynamically allocated array;
int *Array(int length)
{
     return new int[length];
}
This allows you to obtain multiple arrays (each call to the function yields a different array), but the caller has to remember to clean those arrays up to avoid a memory leak.

3) Return a structure that contains an array;
typedef struct {int x[5];} IntArray;

IntArray Array()
{
     IntArray x;
     return x;  
}
The problem with this is that the length of the array must be known at compile time, and accessing elements requires going through a struct (eg the third element will be accessed as array.x[2]).

4) Use a C++ class that encapsulates an array, and provides necessary operators (eg operator[]) to allow direct access of elements. This is an extended form of one of the previous methods. For example;
class IntArray
{
  public:
    IntArray(int size) : array(new int[thesize]), size(thesize) {};
    IntArray(const IntArray &x) : array(new int [x.size]), size(x.size) {};
    IntArray &operator=(const IntArray &x)
    {
         IntArray temp(x);
         int *itemp = temp.array;
         temp.array = array;
         array = itemp;
         int isize = temp.size;
         temp.size = size;
         size = isize;
         return *this; 
    };
    ~IntArray() {delete [] array;};
 
    int &operator[](int index)
    {
        if (index < 0 && index >= size) ComplainBitterly();
        return array[index];
    };

    int operator[](int index) const
    {
        if (index < 0 && index >= size) ComplainBitterly();
        return array[index];
    };

private:
     int *array;
     int size;
   
};

IntArray Array(int size)
{
    IntArray x(size);
    return x;   
}
The obvious issue is that it takes a bit of work.

5) Return a std::vector<int>. This is a special case of the previous method, except that you don't have to roll your own.
grumpy is offline   Reply With Quote
Old Mar 31st, 2006, 7:25 PM   #6
LudakoT
Newbie
 
Join Date: Mar 2006
Location: Macedonia
Posts: 12
Rep Power: 0 LudakoT is on a distinguished road
You can't return a whole array think for a bit how arrays go in memory
Memory location    Value
1                         32
2                         34
3                         36
4                         38
5                         39
Now when you do
int a[10];
SomeFunction(a);
You don't send all 10 elements to the function, that way a lot of memory would be used, when you cout or printf only the name of the array you get the array's beginning address i.e. a[0]. What you can do is send a pointer to the function use that pointer to modify the array and then return the pointer. Of course you can always use void() function that way you don't have to return anything since the array will be modified after the function's execution.
LudakoT is offline   Reply With Quote
Old Apr 1st, 2006, 4:41 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by LudakoT
You can't return a whole array think for a bit how arrays go in memory
But you can encapsulate your array in an object, and that way, you can return even more than a whole array.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC