Hi, I just wondering how to Passing structures of array to functions

Here is my code

#include <stdio.h>

struct harbour 
{
    int harbourId;
    int maxCap;
    int timeUpload;
    double amount;
    
};

struct ship
{
    int number;
    int shipId;
    int sMaxCap;
    int sActCap;
    int aTime;
};
void readHarbour()
{
    int i=0, lineCount=0;
    struct harbour dock[100]; 
	FILE *harbourf;
	harbourf = fopen("data.txt","r");
    do 
    {
        fscanf(harbourf, "%d %d %d %lf", &dock[i].harbourId, &dock[i].maxCap, &dock[i].timeUpload, &dock[i].amount);
        
        lineCount++;
        i++;
    } while (feof(harbourf) == 0);
 
}
void readShip()
{
    int i=0, lineCount=0;
    struct ship ship[100]; 
	FILE *shipf;
	shipf = fopen("data2.txt","r");
    do 
    {
        fscanf(shipf, "%d %d %d %d", &ship[i].shipId, &ship[i].sMaxCap, &ship[i].sActCap, &ship[i].aTime);
        
        lineCount++;
        i++;
    } while (feof(shipf) == 0);

}
void processing(int lineCount)
{
    // pass to here 
}
    
}

int main() 
{
    struct harbour h[100];
    struct ship s[100];
    
    readShip(s);
	readHarbour(h);    
    return 0;
}

Dani AI

Generated

’s problem is that the arrays are created inside readHarbour() / readShip() and vanish when those functions return. As suggested, pass the destination from main — but also pass a capacity, return the number of items read, and check file/scan results. Avoid feof() as the loop condition; use the return value from fscanf() (or read lines with fgets() + sscanf()).

Example pattern (robust, minimal):

#include <stdio.h>
#include <stddef.h>

typedef struct { int id; int capacity; int upload_time; double amount; } Harbour;
typedef struct { int id; int max_cap; int act_cap; int arrival; } Ship;

/* returns number read, 0 on error or empty file */
size_t read_harbours(Harbour out[], size_t max, const char *path)
{
    FILE *f = fopen(path, "r");
    if (!f) return 0;
    size_t n = 0;
    while (n < max) {
        int got = fscanf(f, "%d %d %d %lf",
                         &out[n].id, &out[n].capacity,
                         &out[n].upload_time, &out[n].amount);
        if (got == EOF) break;
        if (got != 4) { /* handle or skip malformed line */ break; }
        n++;
    }
    fclose(f);
    return n;
}

/* processing should accept pointers + counts */
void processing(const Harbour docks[], size_t docks_n,
                const Ship ships[], size_t ships_n);

Key points and troubleshooting:

  • Declare arrays in main (or malloc and return a pointer) and pass them in. Local arrays in a function do not survive return.
  • Always check fopen() and the return value of fscanf(); a mismatched format string will break the loop.
  • Return the actual count read and use size_t for sizes.
  • Use const in processing() when inputs are not modified.
  • If parsing fails, print or log the offending line (use fgets() for that) to debug input-format problems.
  • Keep the format string and file data in sync; small typos in the format are the most common source of silent failures.

Change your readShip and readHarbour function signatures to accept the pointers of your structures.
Example:

typedef struct HARBOUR 
{
    int harbourId;
    int maxCap;
    int timeUpload;
    double amount;
} harbour;

void readHarbour(harbour* docks)
{
	// code goes here
}

int main(void)
{
	harbour docks[100] = {0};
	readHarbour(docks);
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.