Thread: Passing a 2d array by Reference

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Passing a 2d array by Reference

    I created a problem and solved it. Now i rewrote it and try to pass a variable named square by reference. Now im having trouble. What is the right way to pass a 2d array by reference?


    Call by Value - Working
    PHP Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>


    #define SIZE 9


    void createprintdataint [SIZE][SIZE] );
    int hasduplicateint [SIZE][SIZE], int );
    void printmaxint [SIZE][SIZE] );

    int square[SIZE][SIZE] = { };


    int main()
    {
        
    clrscr();
        
    createprintdatasquare );
        
    getch();
        return 
    0;
    }


    void createprintdataint location[SIZE][SIZE] )
    {
        
    int genint 0;
        
    int col 0row 0;

        
    srandtime) );

        for( 
    col 0col != SIZEcol++ ){
            
    gotoxy( ( col ) * 5);
            
    printf"(%d)"col );
        }


        for( 
    row 0row != SIZErow++ ){
            
    gotoxy2,  ( row ) * );
            
    printf"(%d)"row );

            for( 
    col 0genint 0col != SIZEcol++ ){
                while( !
    genint || hasduplicatelocationgenint ) ){
                    
    genint = ( rand() % 99 ) + 1;
                }
                
    location[row][col] = genint;
                
    gotoxy( ( col ) * 5, ( row ) * );
                
    printf"%d"genint );
            }
        }
        
    gotoxy2522 );
        
    printmaxlocation );
    }


    int hasduplicateint location[SIZE][SIZE], int i )
    {
        
    int hasdup 0;
        
    int rowcol;

        for( 
    col 0col != SIZEcol++ ){
            for( 
    row 0row != SIZErow++ ){
                if( 
    location[row][col] == ){
                    
    hasdup 1;
                    break;
                }
            }
            if( 
    hasdup ) break;
        }
        return 
    hasdup;
    }


    void printmaxint location[SIZE][SIZE] )
    {
        
    int rowcol;
        
    int max location[0][0], min maxcurval min;

        for( 
    row 0row != SIZErow++ ){
            for( 
    col 0col != SIZEcol++ ){
                
    curval location[row][col];
                
    max = ( curval max curval max );
                
    min = ( curval min curval min );
            }
        }

        
    printf"\nThe highest value generated is %d"max );
        
    printf"\nThe lowest value generated is %d"min );




    Call by Reference - Not Working!!
    PHP Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>


    #define SIZE 9


    void createprintdataint ** );
    int hasduplicateint **, int );
    void printmaxint ** );



    int main()
    {
        
    int square[12][12] = { };
        
    clrscr();
        
    createprintdatasquare );
        
    getch();
        return 
    0;
    }


    void createprintdataint **location )
    {
        
    int genint 0;
        
    int col 0row 0;

        
    srandtime) );

        for( 
    col 0col != SIZEcol++ ){
            
    gotoxy( ( col ) * 5);
            
    printf"(%d)"col );
        }


        for( 
    row 0row != SIZErow++ ){
            
    gotoxy2,  ( row ) * );
            
    printf"(%d)"row );

            for( 
    col 0genint 0col != SIZEcol++ ){
                while( !
    genint || hasduplicatelocationgenint ) ){
                    
    genint = ( rand() % 99 ) + 1;
                }
                
    location[row][col] = genint;
                
    gotoxy( ( col ) * 5, ( row ) * );
                
    printf"%d"genint );
            }
        }
        
    gotoxy2522 );
        
    printmaxlocation );
    }


    int hasduplicateint **locationint i )
    {
        
    int hasdup 0;
        
    int rowcol;

        for( 
    col 0col != SIZEcol++ ){
            for( 
    row 0row != SIZErow++ ){
                if( 
    location[row][col] == ){
                    
    hasdup 1;
                    break;
                }
            }
            if( 
    hasdup ) break;
        }
        return 
    hasdup;
    }


    void printmaxint **location )
    {
        
    int rowcol;
        
    int max location[0][0], min maxcurval min;

        for( 
    row 0row != SIZErow++ ){
            for( 
    col 0col != SIZEcol++ ){
                
    curval location[row][col];
                
    max = ( curval max curval max );
                
    min = ( curval min curval min );
            }
        }

        
    printf"\nThe highest value generated is %d"max );
        
    printf"\nThe lowest value generated is %d"min );

    Last edited by loko; 07-23-2005 at 05:16 AM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    In C all arrays are passed by reference, that is the variable is the address of the first element of the array. The trouble you may be having in the second program which uses pointer notation is with referencing each element via it's pointer.
    You have to be careful when passing arrays to a function if you do not wish to change the values of the elements of an array. To pass an array to a function without changing the underlying values of the elements pass the array with the keyword const.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by bigtamscot
    In C all arrays are passed by reference, that is the variable is the address of the first element of the array. The trouble you may be having in the second program which uses pointer notation is with referencing each element via it's pointer.
    You have to be careful when passing arrays to a function if you do not wish to change the values of the elements of an array. To pass an array to a function without changing the underlying values of the elements pass the array with the keyword const.

    I need to change the value in function createprintdata() so i didnt put the keyword const..

    The problem im having with the second program is when i try calling printmax and printing the lowest and highest value generated it doesnt generate the correct values.

    Did i accenditally changed the data in var square in function printmax?


    Output
    Code:
             (1)  (2)  (3)  (4)  (5)  (6)  (7)  (8)  (9)
    
    
     (1)     4    70   71   84   57   29   7    74   35
    
     (2)     79   16   31   93   9    11   63   2    86
    
     (3)     88   49   52   56   57   66   68   14   19
    
     (4)     5    51   89   37   79   59   41   32   23
    
     (5)     17   40   13   75   8    74   33   89   43
    
     (6)     46   91   60   23   27   65   40   4    79
    
     (7)     38   37   69   5    62   60   61   7    1
    
     (8)     76   89   84   96   65   56   69   67   37
    
     (9)     23   14   44   45   48   30   34   6    7
    
    
    The highest value generated is 48
    The lowest value generated is 6
    Edit: Seems like the createprintdata() is not working properly too. number 40 was generated twice.
    Last edited by loko; 07-23-2005 at 06:03 AM.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    When trying to get the highest and lowest values for an array of integers, it is a good idea to set the values of high and low prior to the loop where the minimum is the highest value possible and the maximum is the lowest value possible so that they both will change.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by bigtamscot
    In C all arrays are passed by reference, that is the variable is the address of the first element of the array. The trouble you may be having in the second program which uses pointer notation is with referencing each element via it's pointer.
    You have to be careful when passing arrays to a function if you do not wish to change the values of the elements of an array. To pass an array to a function without changing the underlying values of the elements pass the array with the keyword const.

    Okay got what you said now working

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >int square[12][12] = { 0 };
    >createprintdata( square );
    This is fine, and actually passes the array by simulated reference (since C doesn't support that feature at all), but only if createprintdata expects a two dimensional array or anything equivalent.

    >void createprintdata( int **location )
    Okay, a pointer to a pointer is not equivalent to a two dimensional array. These will work:
    Code:
    void createprintdata ( int location[10][10] );
    Code:
    void createprintdata ( int (*location)[10] );
    A pointer to a pointer will work if you simulate a two dimensional array with dynamic memory:
    Code:
    int **square = malloc ( 10 * sizeof *square );
    int i;
    
    for ( i = 0; i < 10; i++ )
      square[i] = malloc ( 10 * sizeof *square[i] );
    
    createprintdata ( square );
    Why all of this? Because, as you may have learned because you tried to take advantage of it, when an array is passed to a function, it is converted to a pointer to the first element. However, that rule only applies to the first dimension. So a[10][10] is converted to (*a)[10], not **a.
    My best code is written with the delete key.

  7. #7
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Is it the same in C++ that all arrays are passed by reference?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it the same in C++ that all arrays are passed by reference?
    Yes, but in C you have to be more careful to make the distinction between passing by reference, and passing by pointer, because C++ does support passing by reference, and it's different.
    My best code is written with the delete key.

  9. #9
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Prelude
    >Is it the same in C++ that all arrays are passed by reference?
    Yes, but in C you have to be more careful to make the distinction between passing by reference, and passing by pointer, because C++ does support passing by reference, and it's different.
    Okay now i know, thnks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  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 array to a function
    By Krupux in forum C Programming
    Replies: 4
    Last Post: 09-04-2003, 07:08 AM