array of struct as function parameter

I'm trying to pass an array of struct to a function, but I'm having a hard time figuring out the correct syntax.

Here's my struct and array definition:
1
2
3
4
5
6
7
8
 struct Item //define struct with three variables
    {
        string name; //item name
        double price; //item price
        int quantity; //item quantity
    };

    Item shoppingCart[100]; //array of struct Item 


Here's my function prototype:
 
void AddItem (struct Item shoppingCart[]);


Here's my function header:
 
void AddItem (struct Item, shoppingCart[])


and here's my function call in main:
 
AddItem (Item, shoppingCart);
Function prototype (and its definition) should have parameters in form: return_type function_name(type1 name1)l 

In your case:
return_type = void 
function_name = AddItem 
type = Item* (as array parameter without fixed size would decay to pointer anyway)
name = shoppingCart 
So it would be void AddItem(Item* shoppingCart) 


You call function as function_name(argument_name) 

In your case:
function_name = AddItem 
argument_name = shoppingCart 
So it would be AddItem(shoppingCart) 
Last edited on
Topic archived. No new replies allowed.