Passing a double array from main to another function

This is a discussion on Passing a double array from main to another function within the C++ Programming forums, part of the General Programming Boards category; Could someone give me an example of how to pass a double array from the main method/function to another function. ...

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    Passing a double array from main to another function

    Could someone give me an example of how to pass a double array from the main method/function to another function. I need to pass by reference. Meaning, what I update in the function called by main needs to be reflected in the original array in the main function. I have tried some stuff with passing by reference and using pointers, but I am having trouble.

    I figured I could "cheat" around this problem by making my two dimensional array global, but my compiler gives me errors saying that I can't instantiate (may not be exact terminology) the array outside of a function.

    I hope this is clear. Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Yet another "My code produces errors" without any mention of either the actual code or the actual errors

    Code:
    void foo ( double x[10][20] ) {
      /* do stuff with x[row][col] */
    }
    int main ( ) {
      double x[10][20];
      foo( x );
    }
    Simply copy the declaration of the array you want to pass as a parameter to the function into the parameter declaration of that function.
    Access the array inside foo() in exactly the same way as you would in main()

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21