How to an array to a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #16
    Then array name and the parameter passed to the function can not be same.
    It should be like.

    Code:
    int determinePage(int virtual_addr_i) // Note the name of the variable is not same as array name
    {
       //Some stuff goes here
    }
    And function call must be like

    Code:
    for (int i=0; i<10; i++)
    {
        cout <<determinePage(virtual_addr[i])<< endl;
    }
    Regards
    Dheeraj Joshi

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #17
      The array name and the function argument name can certainly be the same since they are in sifferent scopes.

      However, the array name is a pointer to element 0. Therefore, the function argument has to be an int* and not an int.

      Comment

      • BenFedorko
        New Member
        • May 2010
        • 8

        #18
        Originally posted by nlal
        Hi i have an array

        Code:
        static int virtual_addr[10]=
        {8196,34893,38583,22883,61843,43532,333,8448,2334,9492};
        that i am trying to pass to a function

        Code:
        int determinePage(int virtual_addr){
          int page_num;
          int page_size = 4096;
        
          page_num = floor(virtual_addr/page_size);
           return page_num;
        }
        however I am getting errors.Any help would be appreciated
        int determinePage(i nt virtual_addr){
        int page_num;
        int page_size = 4096;

        page_num = floor(virtual_a ddr/page_size);
        return page_num;
        }


        your page_num = floor(virtual_a ddr/page_size)
        is setting page_num = floor(array with 10 values divided by page_size);
        you can't use an array like this, you have to pick a value from the array, or make a loop to use every value from the array.


        Code:
        int determinePage(int virtual_addr[]) //<-- the [] MUST be in there
        {
          int page_num;
          int page_size = 4096;
         
          page_num = floor(virtual_addr[0]/page_size); // must choose one value from the array to use or it has ten values and can't use them all like this.
           return page_num; 
        }

        Comment

        • SUNIL TYATA
          New Member
          • Feb 2010
          • 18

          #19
          the function named floor(.......) used is not identified by c++ complier or is it user defined function?

          Comment

          • SUNIL TYATA
            New Member
            • Feb 2010
            • 18

            #20
            IF you dont use the function floor(...) in your code then I made simple solution may it will help you. Thanks
            #include<iostre am>
            using namespace std;
            int determinePage(i nt);


            int main()
            {
            int page_number;
            static int virtual_addr[10]=
            {8196,34893,385 83,22883,61843, 43532,333,8448, 2334,9492};

            cout << "Page Number is: " <<endl;
            for (int i=0; i<10; i++)
            {
            page_number = determinePage(v irtual_addr[i]);
            cout <<page_number << endl;
            }

            system("pause") ;
            return 0;
            }

            int determinePage(i nt virtual_addr)
            {
            int page_num;
            int page_size = 4096;
            page_num = virtual_addr/page_size;
            return page_num;
            }



            out put:
            Page Number is:
            2
            8
            9
            5
            15
            10
            0
            2
            0
            2
            Press any key to continue . . .

            Comment

            Working...