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]