Thread: Passing 2D arrays?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53

    Passing 2D arrays?

    #include "theworld"

    Why is this illegal and what is the appropriate way to do this?

    void try1(char test[][])
    {
    char c[10][10];
    for(int i = 0; i < 10; i++)
    strcpy(c[i], test[i]);
    }
    for(i = 0; i < 10; i++)
    cout<<c[i]<<endl;

    }
    void main()
    {
    char c[10][10];

    for(int i = 0; i < 10; i++)
    {
    strcpy(c[i], "TEST");
    }
    try1(c);

    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53
    I know the sample code is not following any standard, please forgive me.

  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
    The easiest way is to follow the way the array is declared

    Given
    char c[10][20];

    The function prototype would be
    void try1( char array[10][20] );



    Code:
    void try1(char test[10][10]) 
    { 
        char c[10][10]; 
        for(int i = 0; i < 10; i++) 
            strcpy(c[i], test[i]); 
        } 
        for(i = 0; i < 10; i++) 
            cout<<c[i]<<endl; 
    } 
    
    int main() // always int, never void
    { 
        char c[10][10]; 
    
        for(int i = 0; i < 10; i++) 
        { 
            strcpy(c[i], "TEST"); 
        } 
        try1(c); 
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d Int Arrays
    By Voondebah in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 11:36 PM
  2. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  3. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  4. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  5. Passing 2D arrays
    By samGwilliam in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2006, 09:04 PM