How to an array to a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nlal
    New Member
    • Jan 2010
    • 19

    How to an array to a function

    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
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You're passing an array of integers to the function, not a single integer. Your function signature should reflect that: int determinePage(i nt virtual_addr[])

    Comment

    • nlal
      New Member
      • Jan 2010
      • 19

      #3
      Thnaks for your response.
      However i still get errors

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        And the error is?

        Comment

        • nlal
          New Member
          • Jan 2010
          • 19

          #5
          too few arguments to function int determinPage(in t*)
          and
          invalid operands int* and int to binary operator /

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Can you show us the full code, please?

            Comment

            • nlal
              New Member
              • Jan 2010
              • 19

              #7
              well that is the code.
              I have an array of initialized variables and i m trying to pass its values to the function so that i can divide each value by 4096 and return each value to the main function.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                That isn't the complete code. You don't show how you're calling the function, etc., nor the updated function.

                Comment

                • nlal
                  New Member
                  • Jan 2010
                  • 19

                  #9
                  Code:
                  int main()
                  {
                      cout << "Page Number is: " <<endl;
                      for (int i=0; i<10; i++){
                      cout <<determinePage()<< endl;
                      }
                  
                  system("pause");
                  return 0;
                  }
                  however the error is generated in the function determinePage

                  Comment

                  • Dheeraj Joshi
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1129

                    #10
                    Do you have any function prototyping?
                    If yes. Please post it.

                    Regards
                    Dheeraj Joshi

                    Comment

                    • nlal
                      New Member
                      • Jan 2010
                      • 19

                      #11
                      No i don't have it

                      Comment

                      • Dheeraj Joshi
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1129

                        #12
                        Code:
                        cout <<determinePage()<< endl;
                        You are calling a function without arguments.

                        and you say your function takes one argument.
                        Code:
                        int determinePage(int virtual_addr[])
                        Regards
                        Dheeraj Joshi

                        Comment

                        • Dheeraj Joshi
                          Recognized Expert Top Contributor
                          • Jul 2009
                          • 1129

                          #13
                          Are you trying to pass one value at a time to functions from your array?

                          Regards
                          Dheeraj Joshi

                          Comment

                          • nlal
                            New Member
                            • Jan 2010
                            • 19

                            #14
                            yes but my immediate concern is how to correctly pass the parameter to the function without getting errors

                            Comment

                            • nlal
                              New Member
                              • Jan 2010
                              • 19

                              #15
                              yes i am passing one value at a time

                              Comment

                              Working...