Passing Arrays of Structures

Started by
1 comment, last by allsorts46 21 years, 1 month ago
Hi, simple question. I have a structure called LineStruct, which contains a few floats, nothing special. I then have an zero-sized array of these: LineStruct Wall[]; I have a function to which I want to pass the array (a pointer to it anyway). ObjectClass::TestForIntersection(LineStruct* WallArray[]); And I try to pass the array to it with: SomeObjectClassInstance.TestForIntersection(&Wall[]); And it wont even compile. I''ve never actually learnt about this, eveything I do is what I''ve taught myself over the years, and I took a guess that this would be the way it would work... only it clearly isnt. Can someone tell me what''s wrong? Warning in life.cpp (8): const Happiness is declared but never used.
"The finger of blame has turned upon itself"
Advertisement
quote:Original post by allsorts46
Hi, simple question. I have a structure called LineStruct, which contains a few floats, nothing special. I then have an zero-sized array of these:

LineStruct Wall[];

I have a function to which I want to pass the array (a pointer to it anyway).

ObjectClass::TestForIntersection(LineStruct* WallArray[]);

And I try to pass the array to it with:

SomeObjectClassInstance.TestForIntersection(&Wall[]);

And it wont even compile. I've never actually learnt about this, eveything I do is what I've taught myself over the years, and I took a guess that this would be the way it would work... only it clearly isnt. Can someone tell me what's wrong?

Warning in life.cpp (8): const Happiness is declared but never used.

ObjectClass::TestForIntersection(LineStruct* wArray);SomeObjectClassInstance.TestForIntersection(Wall);       

to pass an array you have a couple of options:

int ArrayFunc (int *); // or
int ArrayFunc (int []);

to pass the array just do this in main () :

            int main (){   int weaponArray [10] = {0};   ArrayFunc (weaponArray); // enter array name    //or you can do this... this is probably what you were thinking of   ArrayFunc (&weaponArray[0]);//&weaponArray[0], weaponArray, and &weaponArray all mean the //same thing in this context   return 0;}            

500

edit: how come C-style comment don't work (ie. comments out all the code under it)?

[edited by - Alpha_ProgDes on March 9, 2003 4:19:43 PM]

Beginner in Game Development?  Read here. And read here.

 

Well, an array behaves like a pointer if you pass it as an argument.

In ObjectClass::TestForIntersection(LineStruct* WallArray[]) you say the parameter has to be an array of pointers.

And then you pass it a pointer to an array (= a pointer to a pointer in this context).


So i would change the parameter ''LineStruct* WallArray[]'' to ''LineStruct* WallArray'' and change the argument you pass to ''Wall''

This topic is closed to new replies.

Advertisement