Thread: Passing 2D arrays by reference

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    Passing 2D arrays by reference

    I thought as an array was basically a pointer and 2D array was a pointer to a pointer, it would be possible to pass a 2D array like this:

    Code:
    void function (float **f);
    
    main ()
    {
       float num [5] [8];
    .
    .
    .
       funciton (num);
    }
    But it doesn't work. Why?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    First of all, an Array is NOT a pointer, nor is a 2D Array a pointer to a pointer.

    An Array is more of a table of information. In your Array that you declared you have [5] rows of [8] cells (this is how I remember it).

    If you want to pass the value of an array then you must include it in your function call.

    ie...
    functionname(Array[row_needed][cell_needed]);

    then you must recall the function each time.


    OR

    You can declare your array outside of your main function, thereby rendering it a global variable and you will not have to pass its value from main, you can simply calculate whatever you need and can return it back to main...

    Hope this helps, I'm not expert by far (obviously), but you should read/study up on Arrays.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    and were is your proto function

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Do this:

    Code:
    #include <stdio.h>
    
    void arrayFunc( int Array[][] );
    
    int main()
    {
       int mainAr[10][10];
    
       arrayFunc(mainAr);
    
       return 0;
    }
    
    void arrayFunc( int Array[][] )
    {
       for( int i = 0; i < 10; i++)
          for(int j = 0; j < 10; j++)
             cout << Array[i][j];
    }

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >First of all,

    On the contrary, tho the notation for it's indexing is different than from pointers, if you use the array name [without indexing subscripts], you do in fact return the address of the array, just as in pointers.
    hasafraggin shizigishin oppashigger...

  6. #6
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Yes, an array is a pointer constant as you can't alter the base address - you can access pointers as arrays, too you know. They are interchangeable

  7. #7
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    yeah, num would be a pointer to num[0][0], right?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >First of all, an Array is NOT a pointer, nor is a 2D Array a pointer to a pointer.
    This is a very touchy subject, the relationship between arrays and pointers is close enough to drive people mad when dealing with the errors. It's best to remember that arrays and pointers are not the same, most of the time.

    >functionname(Array[row_needed][cell_needed]);
    Yes and no, while the 2nd - nth dimensions require a size with array notation, the first dimension is optional:
    Code:
    #include <iostream>
    
    void function ( char array[][5], int row, int col )
    {
      for ( int x = 0; x < row; x++ )
        std::cout<< array[x] <<" ";
      std::cout<<"\n";
    }
    
    int main ( void )
    {
      char a[4][5] = {"This","is","a","test"};
      function ( a, 4, 5 );
      return 0;
    }
    >thereby rendering it a global variable and you will not have to pass its value
    Global variables are usually considered a bad thing because they can be accessed by any function in the program. This destroys any data hiding, which is a key factor in C++.

    >They are interchangeable
    Only in very specific situations, such as passing them as arguments to functions.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    on the matter of universal variables: in a program i'm currently working on, i'd have to pass this one object into virtually every function , and there is only one instance of it throughout the program. I decided that it is best for me to make it global to make programming it much easier on myself.

    It's not like i overuse them.. it's only 1 global variable.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I decided that it is best for me to make it global to make
    >programming it much easier on myself.
    That's pretty selfish. You should code for three people: the user, the maintenance programmer, and yourself, in that order. The problem with global variables is that they are in a prime position to cause trouble, thus making a debugging and maintenance run that much more difficult. A little extra effort on your part to make the code correct, easy to read, and easy to maintain can make all the difference in the future.

    >it's only 1 global variable
    Right now it is, but for every global variable you use, there should be about 10 minutes of debate for whether or not you actually need it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. passing dynamicaly allocated 2d arrays
    By s_siouris in forum C Programming
    Replies: 6
    Last Post: 05-25-2008, 04:19 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Passing 2D arrays?
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2002, 03:38 PM