0

how come pass by value doesnt reset an array back to its global while an int pass by value resets back even if you change it in the function? example, items_ordered becomes 0 when i return even though i added 1 but the array doesn't become back to how it started before the function was called. i know if i pass items_ordered by reference it would change

    #include <iostream>
    using namespace std;

    void meal(char menu_1[], int order);
    int main()
    {
      char menu_order[50];
      int items_ordered = 0;
      meal(menu_order, items_ordered);
      cout<<items_ordered<< menu_order;
      return 0;
    }

   void meal(char menu_order[50],int items_ordered)
    {
    cout<< "please enter an item to order\n";
    cin.get(menu_order, 50, '\n'); cin.ignore(100,'\n');
    items_ordered += 1;
    }
Code971
  • 325
  • 1
  • 2
  • 4
  • 1
    You can't pass (built-in) arrays by value. The parameter is a pointer. – chris Apr 26 '15 at 00:48
  • Always fun discussions to fill the air in lunch time conversation! "References are only disguised pointers!" "const char * vs char const * vs const char const *" and last not least: "void f( int *) vs void f(int[]) vs void f(int[3])". – BitTickler Apr 26 '15 at 00:51

0 Answers0