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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// 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:



1
2
3
4
5
int main(){

initialize(ship);

}


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



1
2
3
4
5
6
7
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 []).

1
2
3
4
5
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:

1
2
3
4
5
void initialize(ship[]){

for(int i=0; i<FLEET_SIZE; i++)

ship[i].loc.x = -1;



}

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

1
2
3
4
void initialize(ship s[])
{  for(int i=0; i<FLEET_SIZE; i++)
      s[i].loc.x = -1;
}

Topic archived. No new replies allowed.