How to pass or declare array members in c?

112 Views Asked by At

I developed a program that is supposed to pass the following members of the array of double purchase from the following function:

float beverages ()
{              
   char response;
   double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
   printf("\nEnter your order: ");
   scanf("%c", response);

   if (response == 'l')
   {
      purchase [0];
      printf("You have chosen a Regular Long Black coffee\n");
      printf("that will be %.2f dollars", purchase [0]); 

   }
   else if (response == 'L')
   {
      purchase [1];
      printf("You have chosen a Large Long Black coffee\n");
      printf("that will be %.2f dollars", purchase [1]);

   }
   else if (response == 'f')
   {
      purchase [2];
      printf("You have chosen a Regular Flat White coffee\n");
      printf("that will be %.2f dollars", purchase [2]); 

   }
   else if (response == 'F')
   {
      purchase [3];
      printf ("You have chosen a Large Flat White coffee\n");
      printf("that will be %.2f dollars", purchase [3]); 

   }

   else if (response == 'c')
   {
      purchase [4];
      printf("You have chosen a Regular Cappuccino\n");
      printf("that will be %.2f dollars", purchase [4]);    
   }
   else if (response == 'C')
   {
      purchase [5];
      printf("You have chosen a Large Cappuccino\n");
      printf("that will be %.2f dollars", purchase [5]);        
   }
   else if (response == 't')
   {
      purchase [6];
      printf("You have chosen a Regular Tea\n");
      printf("that will be %.2f dollars", purchase [6]);    
   }
   else  if (response == 'T')
   {
      purchase [7];
      printf("You have chosen a Large Tea\n");
      printf("that will be %.2f dollars", purchase [7]);    
   }

   return purchase[];
}   

and I have it in the int main like:

int main()
{
  ...
  printf("MENU!");
  decision (choice);
  purchase [] = beverages();
  ...
  return 0;
}  

with the following code purchase [] = beverages(); there seems to be a problem where the compiler is saying that purchase is undeclared and there is and unexpected expression before the ] token (by the way the errors are only within the int main). It seems that maybe I have not passed the array from the function properly or something? I've tried adding the value 8 e.g purchase[8] = beverages(); but then the compiler says that purchase is not a pointer or an array. How do I debug this? Am I simply not using right syntax or am I not passing the array correctly?

2

There are 2 best solutions below

3
Amol Saindane On BEST ANSWER

As 'beverages()' function is want to return double array then you need to modify like

  double * beverages()
     {        
        static double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
        // Line of code
        return purchase;
     }

In main function you need to modify like

main()
{
   double *purchase = beverages();     
}

Variable purchase defined in beverages() is local to that function you can not access it in main()

0
2shar On

The lexical scope of the array purchase is within procedure beverages. Instead of trying to access the array outside in main, you can declare it inside main.

int main(void) 
{
      double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
      ... 
      ...
}

Now you can pass the address of the array via pointer to the procedure beverages

void beverages(double *purchase) {

    char response;
    printf("\nEnter your order: ");
    scanf("%c", &response);

    if (response == 'l')
    {
        printf("You have chosen a Regular Long Black coffee\n");
        printf("that will be %.2f dollars", *purchase + 0);

    }
    ...

}

And call the procedure inside main

int main(void)
{

      double purchase [8]= {3.50, 3.80, 3.90, 4.20, 4.00, 4.30, 3.00, 3.10};
;

      beverages(purchase);


}

Also if you want to change the value of the array

*(purchase + i) = value  // where i is index location