#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    1
    Rep Power
    0

    Passing Structures to Function


    I'm passing a structure to a function, and then reading a file which contains the elements needed to fill the structure. But I want to have that reflected up in the main program, so that i can call more functions using the filled structure. But for the life of me, I can't figure out how to change my code to do that. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 8
    
    typedef struct{
    	char location[20];
    	char company[20];
    	int sales_month1;
    	int sales_month2;
    	int sales_month3;
    	int sales_month4;
    	char manager[30];
    	int total_sales;
    	char performance[20];
    }info;
    
    int print_file(/*struct*/ info SALES[], char filename[], char query_comp1[], char query_comp2[]);
    void high_company(info SALES[], int total);
    
    int main(void){
    	int numinput, sum;
    	char filename[20], query_comp1[20], query_comp2[20];
    	info* SALES;
    	SALES = malloc(sizeof(info) * SIZE);
    
    	numinput = print_file(SALES, filename, query_comp1, query_comp2);
    	sum = SIZE + numinput;
    
    	system("PAUSE");
    	return 0;
    }
    
    int print_file(/*struct*/ info SALES[], char filename[], char query_comp1[], char query_comp2[]){
    	int i, read_input, sum;
    	FILE *infile;
    
    	printf("Enter the filename: ");
    	scanf("%s", filename);
    	infile = fopen(filename, "r");
    
    	for(i=0; i<SIZE; i++){
    		fscanf(infile, "%s", SALES[i].location);
    		fscanf(infile, "%s", SALES[i].company);
    		fscanf(infile, "%d", &SALES[i].sales_month1);
    		fscanf(infile, "%d", &SALES[i].sales_month2);
    		fscanf(infile, "%d", &SALES[i].sales_month3);
    		fscanf(infile, "%d", &SALES[i].sales_month4);
    		fscanf(infile, "%s", SALES[i].manager);}
    
    	fscanf(infile, "%s", query_comp1);
    	fscanf(infile, "%s", query_comp2);
    	fscanf(infile, "%d", &read_input);
    	SALES = realloc(SALES, sizeof(info) * (SIZE + read_input));
    	sum = SIZE + read_input;
    
    	for(i=SIZE; i<sum; i++){
    		fscanf(infile, "%s", SALES[i].location);
    		fscanf(infile, "%s", SALES[i].company);
    		fscanf(infile, "%d", &SALES[i].sales_month1);
    		fscanf(infile, "%d", &SALES[i].sales_month2);
    		fscanf(infile, "%d", &SALES[i].sales_month3);
    		fscanf(infile, "%d", &SALES[i].sales_month4);
    		fscanf(infile, "%s", SALES[i].manager);}
    
    	for(i=0; i<sum; i++){
    		printf("%s ", SALES[i].location);
    		printf("%s ", SALES[i].company);
    		printf("%d ", SALES[i].sales_month1);
    		printf("%d ", SALES[i].sales_month2);
    		printf("%d ", SALES[i].sales_month3);
    		printf("%d ", SALES[i].sales_month4);
    		printf("%s\n", SALES[i].manager);
    	}
    
    	for(i=0; i<sum; i++){
    		SALES[i].total_sales = SALES[i].sales_month1 + SALES[i].sales_month2 + SALES[i].sales_month3 + SALES[i].sales_month4;}
    	
    	high_company(SALES, sum);
    	fclose(infile);
    	return read_input;
    }
    
    void high_company(info SALES[], int total){
    	int i, j, greater = 0;
    
    	for(i=0; i<total; i++){
    		if(SALES[i].total_sales > greater){
    			greater = SALES[i].total_sales;
    			j = i;
    			}}
    
    	printf("Highest Sales: %d %s\n", greater, SALES[j].company);
    }
  2. #2
  3. Super User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    648
    Rep Power
    75
    Without testing it, your code appears to do what you are saying you'd like it to do. In C, all values are passed by value, which means that you can edit the function arguments without the changes being reflected in the variables you pass to the function. The exception to this is with arrays: a pointer to the first element is passed as an argument to the function, so when modify an array element in a function, the changes are made to the original array you passed as an argument to that function.

    I'm not sure if that answers your question or not... be a bit more specific as to what output you are expecting, and what you are receiving that differs from what you expect.

IMN logo majestic logo threadwatch logo seochat tools logo