通过故障循环数组在其他源文件中定义 [英] Trouble looping through arrays define in other source files
问题描述
我也问过为什么不能我从一个的malloc /释放calloc(我收到一个答案)。
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).
我现在的问题是,我有2个数组定义,并在两个不同的源文件ship.c和rescue_assets.c填写。我通过他们试图环路一个名为system_handler.c内的方法。
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.
我遇到的问题是,这个任务要求你不要铁杆数组大小到code,所以我不明白我怎么可以链接数组的大小从每个C文件到这个功能在第3 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.
最后,我想:
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.
下面是有问题的code(IVE只添加了必需的片段,所有标题都包括与系统运行按预期杆获得数组的大小):
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
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:资产是我想要得到的大小排列
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:船舶在数组想要得到的尺寸
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));
倒不如用实际的阵列,而不是释放calloc等?
Would it be better to use actual arrays rather than calloc and such?
谢谢,
克里斯。
Thanks, Chris.
推荐答案
您在你分配作为参数传递给函数的项数通过。如果你不能做到这一点(如在您R其中那些在调用的函数分配的情况下),你可以通过添加有作为指针参数,它执行分配的功能(通过引用传递)的大小,或交回返回包含指针和尺寸的结构。
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);
然后在 read_recuse_assets
将 * ASSET_SIZE
来正确的尺寸。
当然,你可以用指针和大小的对面,一个指针传递给 assets_ptr
作为参数和返回的大小。
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.
更完整的例子:
asset *read_recuse_assets(size_t *asset_size)
{
...
*asset_size = no_of_lines;
return assets;
}
如上所述呼叫
有关第二个选择,你可以有这样的结构:
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.
这篇关于通过故障循环数组在其他源文件中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!