簡體   English   中英

如何為結構數組分配內存,然后將該數組傳遞給函數,該函數將在C中填充該數組

[英]How to allocate memory for an array of structs, then pass that array to a function, which populates the array in C

我正在編寫一個程序,試圖在主程序中為結構數組分配內存,然后將該數組傳遞給一個函數,該函數從文件中讀取數據,創建結構並使用創建的結構填充數組。

由於某種原因,這段代碼只能為數組中的7個元素分配足夠的內存。

int assets_size = count_lines("rescue_assets.txt");
asset *assets;
assets = (asset*)malloc(sizeof(asset)*assets_size);

文件中的行數為37,count_lines方法有效,因為它已在主程序中進行了測試。

以下是主要代碼的完整代碼;

int main(int argc, char** argv) {
    int i;
    int assets_size = count_lines("rescue_assets.txt");
    asset *assets;
    assets = (asset*)malloc(sizeof(asset)*assets_size);
    ship ships[100];

    printf("%d\n", assets_size);

    printf("%lu\n", sizeof(assets));

    read_data(assets, ships);

    printf("%lu\n", sizeof(assets));

    for (i=0; i<sizeof(assets)-1; i++) {
        printf("%d\n", assets[i].deployment_time);
    }
    free(assets);
    return (EXIT_SUCCESS);
}

以下是函數read_data的相關代碼;

void read_data(asset *assets, ship ships[]) {
int max_assets;
int max_ships;
FILE *asset_file_ptr;
FILE *ship_file_ptr;
int count = 0;
int file_max = 100;
char asset_file[file_max];
char ship_file[file_max];

asset_file_ptr = fopen("rescue_assets.txt", "r");

if (asset_file_ptr == NULL) {
   printf("The asset file could not be opened."); 
   exit(0);       
}
else {
    char callsign[file_max];
    char type;
    char placename[file_max];
    double base_longitude;
    double base_latitude;
    double speed;
    int deployment_time;
    int service_time; 

    while (fscanf(asset_file_ptr, "%s %c %s %lf %lf %lf %d %d", callsign, &type,    placename, &base_longitude, &base_latitude, &speed, &deployment_time, &service_time)!= EOF) {
        asset* new_asset = malloc(sizeof(asset));
        new_asset->callsign = callsign;
        new_asset->type = type;
        new_asset->placename = placename;
        new_asset->base_longitude = base_longitude;
        new_asset->base_latitude = base_latitude;
        new_asset->speed = speed;
        new_asset->deployment_time = deployment_time;
        new_asset->service_time = service_time;

        assets[count] = *new_asset;
        count++;
    }
}
fclose(asset_file_ptr);

}

這是我從運行主程序得到的輸出;

37 8 8 600 180 180 818 180 600

程序首先打印出asset_size,為37。然后,打印出資產數組的大小,應為37,但應為8。 然后,打印出已填充到陣列中的所有資產的部署時間,僅為7。

請有人能告訴我我要去哪里錯了嗎?

問題出在您的for循環中,主要用於顯示部署時間, sizeof(assets)不能給您數組的長度,沒有辦法獲取僅由簡單指針指向的數組的長度。 sizeof(assets)給出的指針大小為chars,顯然是在64位平台上工作,因此為8。 只需從0運行到asset_size

以及為什么要在read_data為每個資產分配一個新結構,而直接將其直接寫入資產數組。

在主要方面:

for (int i = 0; i < size_assets; ++i)
    printf("%d\n", assets[i].deployment_time);

在read_data中執行:

asset* asset_p = assets;
while (fscanf(...) != EOF) {
    asset_p->field = data;
    ...
    ++asset_p;
}

或者:

int i = 0;
while (fscanf(...) != EOF) {
    assets[i].field = data;
    ...
    ++i;
}

或另選地:

for (int i = 0; fscanf(...) != EOF; ++i) {
    assets[i].field = data;
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM