Trouble looping through arrays define in other sou

2019-09-19 06:46发布

问题:

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.

回答1:

You have to pass in the number of items you have allocated as an argument to the function. If you can't do that (like in you r case where those are allocated in called functions) you can return it by either having the size added as a pointer argument to the function which does the allocation (passing by reference), or by returning a structure containing the pointer and the size.


For the first, you can do something like

size_t asset_size;
asset *assets_ptr = read_recuse_assets(&asset_size);

Then in read_recuse_assets you set *asset_size to the correct size.

Of course, you can do the opposite with the pointer and size, and pass a pointer to assets_ptr as argument and returning the size.

More complete example:

asset *read_recuse_assets(size_t *asset_size)
{
    ...

    *asset_size = no_of_lines;
    return assets;
}

Call as outlined above.


For the second alternative, you can have a structure like this:

struct asset_data
{
    size_t size;
    asset *assets;
};

Then return an instance (not pointer) of this structure with the field filled in.