【问题标题】:How to pass array of structure as a parameter of int function如何将结构数组作为int函数的参数传递
【发布时间】:2021-04-24 17:10:29
【问题描述】:

我想将结构数组作为 int 函数的参数传递,但每次我都尝试这样做。我在 '.' 之前得到预期的 [Error] ';'、',' 或 ')'第 19 行的标记,其中 int 函数名称 add 是。如果我更改为 int add(int totalCost,struct items *input,int quantity) 将有 [Warning] 传递 'add' 的参数 2 使指针从整数而不进行强制转换。

#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 do\n");
            printf("1 - non-rechargeable batteries\n2 - cart summary\n");
            printf("Enter : ");
            scanf("%d",&choice);

            switch(choice)
            {
            case 1:
            {
                int nonRechargeableBatteries;
                printf("\nselect various kind of battery to buy\n1 - Aluminium–air battery\n2 - Bunsen cell\n3 - Dry cell\nAny other number to exit\n");
                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 cancel\n");
                    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("No\tItems%-21sQuantity\tCost\n"," ");

                for(i = 0 ; i<5 ; i++,ptr++)
                {
                    if(a[i]!=0)
                    {
                        printf("%d\t%s%-4s%d\t\t%d\n",input[i].No,input[i].singleitem," ",a[i],(input[i].cost*a[i]));
                    }

                }
                printf("\nTotal Cost\t\t\t\t\t%d\n",totalCost);
                printf("continue shopping Enter\n1 to Add Item\n2 to Delete Items\n3 Add or subtract existing item(s) in cart \nAny other number to Exit\n");
                scanf("%d",&c);
            }

            default:
            {
                if(c==1)
                    printf("Enter Valid Categories Choice\n");
                else;
                break;
            }
            }
            if(choice>0&&choice<2)
            {
                printf("No\tItems%-21sQuantity\tCost\n"," ");
                for(i=0; i<5; i++)
                {
                    if(a[i]!=0)
                    {
                        printf("%d\t%s%-4s%d\t\t%d\n",input[i].No,input[i].singleitem," ",a[i],(input[i].cost*a[i]));
                    }
                }
                printf("\nTotal Cost\t\t\t\t\t%d\n",totalCost);
                printf("continue shopping Enter\n1 to Add Item\n2 to Delete Items\n3 Add or subtract existing item(s) in cart \nAny other number to Exit\n");
                scanf("%d",&c);
            }
            else;
            
        }
        }
    while(c==1 || c==2 ||c==3);
    printf("Your total cost is %d\n",totalCost);}

【问题讨论】:

  • 你正在做一个 nested 函数:int main() { int add() {} } 充其量只是一个扩展,标准不支持。把add 上面 main: int add() { } int main() { }

标签: c


【解决方案1】:

如果我更改为 int add(int totalCost,struct items *input,int quantity) 将有 [警告] 传递 'add' 的参数 2 使指针从整数而不进行强制转换。

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

                        totalCost = add(totalCost, input, quantity);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多