2D arrays with functions made easy

This is a discussion on 2D arrays with functions made easy within the C Programming forums, part of the General Programming Boards category; hi folks, i thought i should share something with people who find it difficult to work with 2D arrays using ...

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

    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 :

    #include<stdio.h>

    void dosomething(char (*virtualarray) [11])
    {
    ...
    /* anything you want to do to virtualarray */
    ...
    return;
    }


    main()
    {
    char mainarray[11][11];

    ...

    dosomething(mainarray);

    ...
    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 ...

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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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] );
    or
    &nbsp; void dosomething ( char (*array) [11] );

    And the function declaration as
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21