1

I have defined these 2 structs:

#define MAP_SIZE 5

typedef struct battle_cell {
  int status_a;
  int status_b;

  int ship_a;
  int ship_b;
} battle_cell;

struct battlemap {
  battle_cell cell[MAP_SIZE][MAP_SIZE];
  int progress_a;
  int progress_b;
};

After the initalization of the map and all the other variables with zeros:

for (i = 0; i < MAP_SIZE; i++) {
  for (j = 0; j < MAP_SIZE; j++) {
    map->cell[i][j].status_a = 0;
    map->cell[i][j].status_b = 0;
    map->cell[i][j].ship_a = 0;
    map->cell[i][j].ship_b = 0;
  }
}

map->progress_a = 0;
map->progress_b = 0;

There is a point that I have to check the ship_a and ship_b values that live in each cell, something like that (the logic is a bit more complex than this iteration):

for (i = posXB; i < posXB + SHIP_SIZE; i++) {
  map->cell[posYB][i].ship_b = 1;
}

I need to do exactly the same for the ship_a variable. So, I have to duplicate quite a big chunk of code because I am not able to find a way to get the field within the struct dynamically. For example, I could define a function:

void cell_iteration (battlemap *map, int pos, int pos_y, int ship_size, /* field_parameter/pointer */) {
  int i;
  for (i = pos; i < pos + ship_size; i++) {
    map->cell[pos_y][i].ship_b = 1; // use the field_parameter/pointer instead of ship_b
  }
}

Is there an elegant way to do something like that?

UPDATE

Just a clarification. The structs can definitely be simplified, but this is not my question. I 've just tried to create an example :)

8
  • 1
    You can probably simplify the data structure. Maybe you could define a ship structure that contains the position(s) of a ship, rather than put so much inside the battle_cell struct. Commented Oct 6, 2016 at 18:49
  • @Stuart definitely the structs can be simplified and this is what the guy below answered. I 've tried to simulate a complicated case just to give an example. I think that I have to edit the question to clarify this :) Commented Oct 6, 2016 at 18:53
  • I'm probably not understanding your question because I don't see how creating an array[2] of ships inside battle_cell helps you. I'm thinking that instead of a field parameter pointer you just need a parameter that indicates which ship you want to access and then have the code access either or maybe both ships. Commented Oct 6, 2016 at 19:00
  • I don't know what you mean by "access dynamically the properties of the struct". Commented Oct 6, 2016 at 19:02
  • @Stuart Defining an array[2], you can make an assumption that the 0 position will be ship_a and position 1 will be ship_b. Then you can pass simple integer 0 and 1 and will be able to access the fields (at least this is what I understood). I am not 100% sure if I understood your suggestion, though. Is it possible to create a very small snippet and post it as an answer? Commented Oct 6, 2016 at 19:08

1 Answer 1

3

Instead if ship_a and ship_b int the first struct, you can declare int ship[2] an array of 2 int.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a solution that can work, but I would really like to know if there is a way to access dynamically the properties of the struct.
I thought the compiler is smart enough to do this on his own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.