如何将结构的数组作为int函数的参数传递



我想把结构的数组作为int函数的参数传递,但每次尝试都会得到预期的[Error]';',','或"("之前的"。"第19行,int函数名称add所在的标记。如果我更改为int add(int totalCost,struct-items*input,int quantity(,将有[Warning]传递'add'的参数2,使指针从integer变为不带强制转换。

#include<stdio.h>
#include<string.h>
struct items
{
int No;
char singleitem[29];
int cost;
};
struct items input[5] = { {0, "Aluminium-air battery",355}, { 1, "Bunsen cell",500}, { 2, "Dry cell",550 }, { 3,"Galvanic cell",350},
};
struct items* ptr = input;
int main()
{
int nonRechargeableBatteries,i=0,choice,c=1,a[10],totalCost=0,quantity=0;
int add(int totalCost,struct items input[i].cost,int quantity)
{
totalCost+=input[i].cost*quantity;
return totalCost;
}
for(i=0; i<5; i++)
a[i]=0;
do
{
//C is 1 by default
if(c==1)
{
printf("npress number in front of command that you would like to don");
printf("1 - non-rechargeable batteriesn2 - cart summaryn");
printf("Enter : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int nonRechargeableBatteries;
printf("nselect various kind of battery to buyn1 - Aluminium–air batteryn2 - Bunsen celln3 - Dry cellnAny other number to exitn");
scanf("%d",&nonRechargeableBatteries);
switch(nonRechargeableBatteries)
{
case 1:
{
int num;
printf("You chose Aluminium–air battery.nPress 1 to add to the cart.nAny other number to canceln");
scanf("%d",&num);
if(num==1)
{
printf("enter how many items you want : ");
scanf("%d",&quantity);
a[0]+=quantity;
totalCost = add(totalCost,input[0].cost,quantity);
}
break;
}
.
.
.
}
break;
}
case 2:
{
printf("NotItems%-21sQuantitytCostn"," ");
for(i = 0 ; i<5 ; i++,ptr++)
{
if(a[i]!=0)
{
printf("%dt%s%-4s%dtt%dn",input[i].No,input[i].singleitem," ",a[i],(input[i].cost*a[i]));
}
}
printf("nTotal Costttttt%dn",totalCost);
printf("continue shopping Entern1 to Add Itemn2 to Delete Itemsn3 Add or subtract existing item(s) in cart nAny other number to Exitn");
scanf("%d",&c);
}
default:
{
if(c==1)
printf("Enter Valid Categories Choicen");
else;
break;
}
}
if(choice>0&&choice<2)
{
printf("NotItems%-21sQuantitytCostn"," ");
for(i=0; i<5; i++)
{
if(a[i]!=0)
{
printf("%dt%s%-4s%dtt%dn",input[i].No,input[i].singleitem," ",a[i],(input[i].cost*a[i]));
}
}
printf("nTotal Costttttt%dn",totalCost);
printf("continue shopping Entern1 to Add Itemn2 to Delete Itemsn3 Add or subtract existing item(s) in cart nAny other number to Exitn");
scanf("%d",&c);
}
else;

}
}
while(c==1 || c==2 ||c==3);
printf("Your total cost is %dn",totalCost);}

如果我更改为int add(int totalCost,struct-items*input,int quantity(,将有[Warning]传递'add'的参数2,使指针从integer变为不带强制转换。

当然,如果将函数参数input声明为struct items *类型,则不能传递int参数input[0].cost,而是传递声明的struct items *

totalCost = add(totalCost, input, quantity);

最新更新