Can you return an Array ???

Started by
17 comments, last by exodus7 20 years, 10 months ago
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( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Advertisement
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;
}
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]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
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.
there is only one way
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:


    // functionsbool 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 10Alien 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]
Brianmiserere nostri Domine miserere nostri
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.

there is only one way
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 isstruct Alien{  int x;  int y;  int strength;  int color;}// create an array of 10 aliensAlien aliens[10];// pass the array of aliens to a functionDoSomething(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 functionDoSomething(&(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]
Brianmiserere nostri Domine miserere nostri
I guess so. Like you said i''m not to good with pointers.
there is only one way
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.

Brianmiserere nostri Domine miserere nostri

This topic is closed to new replies.

Advertisement