Structures Help

I am having trouble with structures in a battleship project. The instructor gave us a header file with all of the function prototypes we have to use and I am having trouble with the first one, "initialize". In the header file we have to use, the instructor has this:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````// // data structures definitions // const int FLEET_SIZE=5; // number of battleships const int FIELD_SIZE=5; // the field (ocean) is FIELD_SIZExFIELD_SIZE // coordinates (location) of the ship and shots struct location{ int x; // 1 through FIELD_SIZE char y; // 'a' through FIELD_SIZE }; // contains ship's coordinates (location) and whether is was sunk struct ship{ location loc; bool sunk; }; // // initialization functions // void initialize(ship[]); // places all ships in -1 X location to signify // that the ship is not deployed location pick(void); // generates a random location bool match(ship, location); // returns true if this location matches // the location of the ship // returns false otherwise int check(const ship[], location); // returns the index of the element // of the ship[] array that matches // location. Returns -1 if none match // uses match() void deploy(ship[]); // places an array of battleships in // random locations in the ocean ``````

I am starting with the initialize function in the main function and am wondering what exactly is going on. So the function Initialize takes the structure ship which as an array size of the a new variable (the number of ships deployed) right? So is this where we initialize all 5 of the ships to have a value of -1? In the main function, when i do something like this:

 ``12345`` ``````int main(){ initialize(ship); }``````

it doesn't compile. So then I tried this:

 ``1234567`` ``````int main(){ ship s; initialize(s); }``````

and it still didn't compile. I am wondering how I can pass the structure in a function if these ways I have tried don't work.
Last edited on
initialize is expecting an array of ships (note the []).

 ``12345`` ``````int main() { ship ships[FLEET_SIZE]; initialize(ships); }``````

Okay that makes much more sense, thank you. Now when I try to make this function I am getting an error saying (Error: expected an identifier) Here is my code:

 ``12345`` ``````void initialize(ship[]){ for(int i=0; i

}

I am not sure what I am doing incorrect.
Line 1. ship is a type. You need an argument name also.

 ``1234`` ``````void initialize(ship s[]) { for(int i=0; i

Topic archived. No new replies allowed.