0

I would like to pass array of structure as a parameter of int function but every time I try to do it. I get the [Error] expected ';', ',' or ')' before '.' token on line 19 where the int function name add is. If I change to int add(int totalCost,struct items *input,int quantity) there will be [Warning] passing argument 2 of 'add' makes pointer from integer without a cast.

#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);}
1
  • 1
    You are doing a nested function: int main() { int add() {} } which is an extension at best, not supported by the standard. Put add above main: int add() { } int main() { } Commented Apr 24, 2021 at 17:33

1 Answer 1

1

If I change to int add(int totalCost,struct items *input,int quantity) there will be [Warning] passing argument 2 of 'add' makes pointer from integer without a cast.

Of course if you declare the function parameter input to be of type struct items *, you must not pass an int argument input[0].cost, but a struct items * as declared:

                        totalCost = add(totalCost, input, quantity);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.