I have asked a question somewhat related to this this before asking why cant i return the array size from a malloc/calloc (i have received an answer to this).
My current question is i have 2 arrays defined and fill in two separate source files ship.c and rescue_assets.c. I am attempting to loop through them in a method inside a file called system_handler.c.
The trouble i am having is that this task requires that you DO NOT hardcore an array size into the code so i don't see how i can link the array size from each c file into this function in the 3rd c file.
Ultimately i would like:
assign_mayday_to_ships(int SIZE_OF_ARRAY_FROM_FILE_1, int SIZE_OF_ARRAY_FROM_FILE_2){
for(int i=0; i < SIZE_OF_ARRAYFROM_FILE_1; i++){
for(int j = 0; < SIZE_OF_ARRAYFROM_FILE_2; j++{
//do something
}
}
i could easily do this if they were in the same file, but i can't call that method from two different files, because it would obviously lack the parameters required.
Here is the code in question ( ive only added the required snippets, all headers are included and the system runs as intended bar getting the array sizes):
system_handler.c
void assign_mayday_to_ships() {
mayday_call* mday_ptr;
ship* ship_ptr;
rescue_asset* assets_ptr;
mday_ptr = read_mayday_file();
ship_ptr = read_ship_locations();
assets_ptr = read_recuse_assets();
int i;
int result;
/* loop through ship locations to find the ship that called the mayday
When found assign the mayday call to the ship for use when sending help*/
for (i = 0; i < arr_size; i++) {
result = strncmp(mday_ptr->ais, (ship_ptr + i)->ais, COMPARE_LIMIT);
if (result == 0) {
mday_ptr->ship = (ship_ptr + i);
}
}
calc_distance_to_mayday(mday_ptr, assets_ptr);
}
rescue_asset.c: assets is the array i want to get the size of.
rescue_asset* assets;
no_of_lines = count_lines(locof);
printf("number of lines = %d \n", no_of_lines);
assets = calloc(no_of_lines,sizeof (rescue_asset));
ship.c: ships is the array want to get the size of.
ship* ships;
/* -1 because first line of file is not a ship location*/
no_of_lines = (count_lines(locof) - 1);
ships = calloc(no_of_lines, sizeof (ship));
Would it be better to use actual arrays rather than calloc and such?
Thanks, Chris.