Start New Discussion within our Software Development Community

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;
}

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;
}
This article has been dead for over six months. Start a new discussion instead.