Thread: passing arrays/pointers to functions

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    passing arrays/pointers to functions

    let's say i have this code:
    Code:
    int main() {
    int size = 6;
    char ez[size][size];
    //fill up 2d array ez with some stuff
      deal_with(ez,size);
    }
    
    void deal_with( ????? , int size) {
    
    
    
    }
    what kind of thing do i pass to the function? i've tried passing it as a 2d array, but since size is not predetermined, it can't guess the size of the array. i've tried a char** but it gives me an error. any ideas?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i've tried a char** but it gives me an error.
    Understandable. When someone sees that an array passed to a function can be passed as either

    char array[]
    or
    char *array

    They feel that a similar method can be taken with multidimensional arrays:

    char array[][10]
    or
    char **array

    But, in reality, two dimensional arrays are degraded into a pointer to an array:

    char array[][10]
    becomes
    char (*array)[10]

    In your case, the use of a structure would minimize headaches. Create the array in a structure, along with the size of each dimension and then allocate the required space at run-time. When you want to pass the array, all you have to do is pass the struct instance and not have to worry about the annoying syntax of 2D arrays as function parameters.

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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not quite perfect, but it seems to work

    Code:
    #include <iostream.h>
    
    void foo ( int size, void *p ) {
        int (*arr)[size] = (int(*)[size])p;
        cout << "Size is " << size << endl;
        int r, c;
        for ( r = 0 ; r < size ; r++ ) {
            for ( c = 0 ; c < size ; c++ ) {
                cout << arr[r][c] << " ";
            }
            cout << endl;
        }
    }
    
    int main ( ) {
        int r,c;
    
        int size = 5;
        int arr[size][size];
        for ( r = 0 ; r < size ; r++ ) {
            for ( c = 0 ; c < size ; c++ ) {
                arr[r][c] = r * c;
            }
        }
        foo ( size, arr );
    
        int size2 = 8;
        int arr2[size2][size2];
        for ( r = 0 ; r < size2 ; r++ ) {
            for ( c = 0 ; c < size2 ; c++ ) {
                arr2[r][c] = r * c;
            }
        }               
        foo ( size2, arr2 );
    
        return 0;
    }
    The obvious
    void foo ( int size, int (*arr)[size] );
    results in complaints about scope.

    From a type point of view, each size would be a different function. You can't pass a [2][2] array to a function which is expecting a [3][3] array.

    The downside is, the array is no longer checked, since its being passed to a void* parameter.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Hrrm I still have to look up some reference on structures (going to use this site's tutorial)
    but - Prelude you're suggesting something like..

    struct ...
    {
    int min=MIN;
    int max=MAX;
    char array[MIN][MAX];
    }

    yes?

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Ohh never mind... I just went through the tutorial and finished looking through the message board more...

    struct name{
    char [5][25];
    }
    OS: Windows ME, Windows XP Pro
    Development Environment: MS VC++ 6.0 (SP3)
    Current Projects:
    none

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The thing is, you want the function to accept either a char** or a char*[]. You're just thinking too much

    EZ:

    Code:
    
    void deal_with(char **strings, int max)
    {  
      int i = 0;
      
      printf("Printing From Within...");
    
      for(i = 0; i < max; i++)  printf(strings[i], i + 1);   
    }
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
      int size = 100;
    
      char ez[ size ][ size ], *pointers[ size ];
    
      int i = 0;
    
      for(i = 0; i < size; i++) pointers[i] = strcpy(ez[i],"String #%i\n");
    
      deal_with( pointers, size );
    
      getch();
      
    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  2. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  3. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM