Thread: 2D arrays with functions made easy

  1. #1
    Registered User goran's Avatar
    Join Date
    Sep 2001

    Lightbulb 2D arrays with functions made easy

    hi folks,

    i thought i should share something with people who find it difficult to work with 2D arrays using functions. Notice the following :


    void dosomething(char (*virtualarray) [11])
    /* anything you want to do to virtualarray */

    char mainarray[11][11];



    return 0;

    anything you do to virtualarray in dosomething gets reflected in mainarray in function main().

    In dosomething, you can have random access by
    virtualarray[i] [j] which points to the jth element of ith row

    Alternatively, you can use (*virtualarray) [j] pointing to the jth element of whichever row virtualarray is initialised to (which is the first row when you enter dosomething). you can increment virtualarray to point to the next row if you need sequential access. To beginners, i would say... use the previous method incrementing 'i' each time to move on to the next row. Using the second method can really bug you if not done properly. I myself ran into trouble trying it out

    Actually i m still in the process of figuring out how this works cause i saw it in a solved example in a book. Suggestions please ...

    I don't wait for the future; it comes soon enough.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    It's actually much much simpler than that - if you take the approach of writing slightly redundant declarations. It saves worrying when to use the (), and how to remove the correct [] term from the array. In essense, it just becomes a cut/paste of the declaration of the n-dimensional array you want to pass as a parameter.

    If you have
    &nbsp; char mainarray[11][11];

    The function prototype can be written as
    &nbsp; void dosomething ( char array[11] [11] );
    Because it is equivalent to
    &nbsp; void dosomething ( char array[] [11] );
    &nbsp; void dosomething ( char (*array) [11] );

    And the function declaration as
    void dosomething ( char array[11][11] ) {
        int i, j;
        for ( i = 0 ; i < 11 ; i++ ) {
            for ( j = 0 ; j < 11 ; j++ ) {
                 array[i][j] = 0;
    The array[i][j] you use in the function is exactly the same method you would use if you were directly accessing the array in main - because that is all you are doing - accessing the array in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 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 2 dimensional arrays to functions
    By homeyg in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2005, 03:16 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM