Archived

This topic is now archived and is closed to further replies.

exodus7

Can you return an Array ???

Recommended Posts

Julio    116
usually you return it through itself by passing it by reference. like this:
void func(int* &array)
{
//do stuff to array
}
or this probably will work also
int * func()
{
int * a;

return a;
}

My Homepage

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Could you clarify that question a little bit

Share this post


Link to post
Share on other sites
micepick    122
Julio, you don''t need to pass an array by reference. In fact, that''s remarkably pointless. The second one you have will definitely not work, since you''d have to allocate the array using new, and then rely on the calling code to delete the array it gets. That is a very, very bad idea.

Share this post


Link to post
Share on other sites
micepick    122
Here is how you work with an array:

    
void ModifyArray(int* array, int size)
{
for (int i=0; i<size; i++)
do something to array[i];

// since the array was passed as a pointer,

// modifying it in here will affect the array

// in the calling code.

}

That's it.


[edited by - micepick on May 26, 2003 1:05:00 PM]

Share this post


Link to post
Share on other sites
Enselic    829
If you doesn''t concern about speed, you can pass a whole struct containg an array:

struct Wazza
{
int data[100];
};

void SomeThing( Wazza whole_struct );




____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10.
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.

Share this post


Link to post
Share on other sites
micepick    122
Or, if you don't want to affect the array that's passed in:

            
int* ArrayOp(int* out, const int* in, int size)
{
// assume the array 'out' was already allocated


for (int i=0; i<size; i++)
out[i] = some_operation(in[i]);

// have the function return a pointer to the output as well,

// just for convenience

return out;
}

Note that this is analogous to functions like memcpy, which are declared thus:

void* memcpy(void *dest, const void *src, size_t size)


[edited by - micepick on May 26, 2003 1:17:25 PM]

Share this post


Link to post
Share on other sites
BriTeg    168
quote:
Original post by exodus7
Hi
Was just wondering if it''s possible to pass and return the contents of everyting in a array.


Yes, but I cannot think of a reason you would ever want to. Normally you pass/return a pointer, or use references.

Share this post


Link to post
Share on other sites
brassfish89    122
quote:

Yes, but I cannot think of a reason you would ever want to. Normally you pass/return a pointer, or use references.


i can. i once saw a function that takes in an error message (directX, to be exact) and returns the an array of what error it is. heres what it looked like:

  
char* GetErrorMsg(HRESULT hr)
{
switch(hr)
{
case DDERR_LOSTDEVICE:
return "DDERR_LOSTDEVICE";
...
}
}

btw i dont even think that error msg exists i was just using it as an example



doh, nuts. Mmmm... donuts
My website

Share this post


Link to post
Share on other sites
BriTeg    168
quote:
Original post by brassfish89
i can. i once saw a function that takes in an error message (directX, to be exact) and returns the an array of what error it is. heres what it looked like:

char* GetErrorMsg(HRESULT hr)
{
...




Note the return type: char *. Even though the string (char array) is hard-coded, the function is returning a pointer to it, ie. the address of where it sits in memory at runtime.

[edited by - BriTeg on May 26, 2003 12:47:10 AM]

Share this post


Link to post
Share on other sites
As Briteg said, the string "DDERR_LOSTDEVICE" resides on the stack and the GetErrorCode function just returns a pointer to it, it does not actually return an entire array.

Instead of returning arrays, you should pass them as function parameters. Like:

int Array [64];
SetArrayValues( Array, 64 );

...

void SetArrayValues ( int* Array, int Size )
{
for( int n = 0; n < Size; n ++ )
Array[n] = n;
}


~CGameProgrammer( );

Share this post


Link to post
Share on other sites
deathtrap    364
when you declare an array like MyArray[10] , MyArray is actually a pointer to the first element of the array.

an array name is just a pointer to the first element, so when you want to pass and return an array, just pass and return the name, just as you would do as if you were passing and returning a pointer.

int * myfunction(int * hi)
{
//to get the value in the third element of the array hi
hi[2];

//to return the array
return hi;
}

Share this post


Link to post
Share on other sites
Lektrix    106
quote:
Original post by deathtrap
when you declare an array like MyArray[10] , MyArray is actually a pointer to the first element of the array.

an array name is just a pointer to the first element, ...


Basically, yes, but bear in mind:

int array[10];
int* pArray = array;

std::cout << "sizeof array:  " << sizeof array  << std::endl
          << "sizeof pArray: " << sizeof pArray << std::endl;


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 27, 2003 7:53:04 AM]

Share this post


Link to post
Share on other sites
exodus7    122
Ok Basically here''s my probelm. I want to check when the first object within my array has reached a certain point. Once that object has reached that point I want to move all the objects within that array.

Here''s the code I have:

Aliens[j] = ShiftAliens(Aliens[j],j);

//Function
ShifrAliens(Alien &Aliens, int Num)
if (Alien.rect.left < m_WinRect.left)
{
Alien.dir=m_DevStepSize;
Alien.point.Offset(CPoint(0,48));
InvalidateRect(Alien.rect);

return Alien;
}
This is only returning them one at a time.

Share this post


Link to post
Share on other sites
BriTeg    168
quote:
Original post by exodus7
Here's the code I have:



That's not the code you have, it won't even compile.

OK, let me try to sort it out. It looks like Aliens is an array of Alien objects. I *think* you would do something like this:


    

// functions

bool CheckLimit(Alien *alien)
{
if(aliens[0].rect.left < m_WinRect.left)
{
// limit has been reached

return true;
}
return false;
}

void ShiftAliens(Alien *aliens, int num) // aliens points to the first element in your array

{
int i;

for(i=0;i<num;i++)
{
aliens[i].dir = m_DevStepSize;
aliens[i].point.Offset(CPoint(0,48));
InvalidateRect(aliens[i].rect);
}
}

// calling code

...
#define NUM_ALIENS 10

Alien aliens[NUM_ALIENS];
int numaliens = NUM_ALIENS;
...
if(CheckLimit(aliens)) // have we reached a limit?

{
// yes, move all the aliens

ShiftAliens(aliens, numaliens);
}
...



[edited by - BriTeg on May 27, 2003 1:06:30 PM]

Share this post


Link to post
Share on other sites
exodus7    122
I can''t get that to work, there''s a compile error when I try to return all the aliens.

ShiftAliens(aliens, numaliens);

error:
cannot convert parameter 1 from ''struct aliens[9]'' to ''struct aliens &''

is this because one is a reference and the other is a pointer????
I''ve tried changing them both to pointers but I get loads of complie errors.

Share this post


Link to post
Share on other sites
BriTeg    168
quote:
Original post by exodus7
I can't get that to work, there's a compile error when I try to return all the aliens.



You don't need to return all the aliens. Simply pass a pointer to your array to the function, which acts on them. Look at what my code example is doing, including the ShiftAliens function.

quote:

ShiftAliens(aliens, numaliens);

error:
cannot convert parameter 1 from 'struct aliens[9]' to 'struct aliens &'

is this because one is a reference and the other is a pointer????



That's because one is an array of aliens, the other is a reference to a single alien.

I don't think you'll fully understand this until you better understand pointers. A pointer is simply a variable that contains a memory address. That memory address can be the address of an array (actually, the address of the first element in the array), the address of any single element in the array, the address of a stand-alone object (not in an array), itself, random space, anything. If you have an array of structs, and you want a function to manipulate that array, you do something like this:


  
// describe what an alien is

struct Alien
{
int x;
int y;
int strength;
int color;
}

// create an array of 10 aliens

Alien aliens[10];

// pass the array of aliens to a function

DoSomething(aliens, 10); // pass a pointer to the first alient in the array, and tell the function that our pointer points to 10 aliens


// pass a single element of the array to the same function

DoSomething(&(aliens[3]), 1); // pass a pointer to the 4th alien in the array, and tell the function that our pointer only points to 1 alien



The function itself would look something like:


  
void DoSomething(Alien *alien_array, int num_in_array)
{
int i;

for(i=0;i<num_in_array;i++)
{
alien_array[i].color = rand() % 5; // random color

alien_array[i].strength = rand() % 10; // random strength

}
}


Does that make sense?

[edited by - BriTeg on May 27, 2003 2:34:25 PM]

Share this post


Link to post
Share on other sites
BriTeg    168
Just study it a little more, you''ll get it! In fact, keep plugging away at this aliens thing, thinking carefully about each step. When I was learning C, the way I finally understood pointers was to solve problems just like this.

Share this post


Link to post
Share on other sites