Thread: Passing an Array of Structures

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Passing an Array of Structures

    Code:
    struct inventory {
        char desc[25];
        int prodID;
        int prodAmt;
        float prodPrice;
    };
    
    struct transaction {
        char desc[25];
        int prodID;
        int prodQuan;
        float prodPrice;
        float totPrice;
    };
    
    struct inventory invDB[INVMAX];
    struct transaction traDB[TRAMAX];
    
    void findAmt(struct inventory invDB, int *invDB_amt, struct transaction traDB, int *traDB_amt); 
    
    main() 
    {
    findAmt(invDB, &invDB_amt, traDB, &traDB_amt);
    }
    
    void findAmt(struct inventory invDB, *invDB_amt, struct transaction traDB, *traDB_amt)
    {
        int inv_count, tra_count, x;
        
        inv_count = 0;
        for (x = 0; x < 1000; x++) {
            if (invDB[inv_count].prodID == 0) {
            break;
            }
            if (invDB[inv_count].prodID != 0) {
            inv_count++;
            }
        }
        *invDB_amt = inv_count;
        
        tra_count = 0;
        for (x = 0; x < 1000; x++) {
            if (traDB[tra_count].prodID == 0) {
            break;
            }
            if (traDB[tra_count].prodID != 0) {
            tra_count++;
            }
        }
        *traDB_amt = tra_count;
    }
    I'm getting errors about incompatable type, for argument 1 and 3 of findAmt... I'm not sure what the correct way of passing the array is...

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The way you have it would work fine if you were passing a single struct. For an array you need to specify the parameter as
    Code:
    struct inventory invDB[]
    Also in your function definition you left off the type of the pointer. Should be 'int *' not just *.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct inventory invDB[INVMAX];
    struct transaction traDB[TRAMAX];
    
    void findAmt(struct inventory invDB, int *invDB_amt, struct transaction traDB, int *traDB_amt); 
    
    main() 
    {
    findAmt(invDB, &invDB_amt, traDB, &traDB_amt);
    }
    You have no such thing as "invDB_amt" and "traDB_amt". There are no such variables in your program. There are however, these variables in the prototype of the function. That's probably where your confusion is.

    Code:
    void findAmt(struct inventory invDB, *invDB_amt, struct transaction traDB, *traDB_amt)
    You can't do that either. You have to have the whole thing in there:

    Code:
    void findAmt(struct inventory invDB, struct inventory *invDB_amt, struct transaction traDB, struct inventory *traDB_amt)
    Of course, in your prototype, you have those two as int. So, you'll need to decide what it is you want. Next, since those two particular variables still don't exist, you'd have to create int variables to pass to them.

    However, since I assume you're really trying to pass your arrays over...
    Code:
    findAmnt( invDB[ONE_INSTANCE], invDB, traDB[ONE_INSTANCE], traDB );
    This would be passing 1 single instance, followed by passing the array, followed by one instance, followed by the whole array.

    However, since you're actually using global variables, there's no real point to passing anything anyway...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    19
    Sweet, thanks so much. I always forget to put the argument variable types in... And the [] for arrays...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help passing nxn array to 2d array.
    By beglaryanh in forum C Programming
    Replies: 2
    Last Post: 06-06-2009, 05:23 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 02:58 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM