Thread: Passing 2d arrays to a function

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Passing 2d arrays to a function

    OK, I think I've lost all my C++ knowledge here. i can't properly pass a 2D array of floats to a function.

    I have a dynamically allocated 2d array of float and I have a function that should fill it. The size is known before the call for the function.
    I have this:
    the function deffinition
    Code:
    void GetData(FILE *ptr, FILE *out, unsigned int Mask[], float &mtxVicVals, int n , int s)
    {
    ........
                                 if(Mask[j] == 1)
    				{
    					mtxVicVals[i][p] = Values[j];
    					p++;
    				}
    .............
    }
    the function call
    Code:
    GetData(inp, out, Mask, mtxVicVals,  vars.NumParam, vars.NumScan);
    I know I'm thinking of it as a 1D array. Will I use two & in th declaration?
    Last edited by earth_angel; 07-18-2005 at 08:58 AM.
    Everything is relative...

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    how about this?
    float **mtxVicVals

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    it compiles, but I get a run-time error that memory could not be written.
    but would passing by reference be faster?
    Everything is relative...

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    let me see...

    is it because i or p out of range of the array

    a referrence won't be faster in this case
    Last edited by Antigloss; 07-18-2005 at 09:16 AM.

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    try this if you intend to use referrence
    Code:
    void a(int (&i)[5][5])
    {}
    
    int main()
    {
        //what goes here?
    	int b[5][5] = { 0 };
    	a(b);
    }

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I did create the matrix with the wrong size. I did use ** in the declaration at first but this error made me start playing with it, and I just got super confused.

    It seems to be working fine now.

    Thanks,

    AS.
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Passing 2d arrays to functions
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 05-06-2005, 05:22 PM
  4. passing 2d array in to function
    By Kings in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 11:35 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM