How do I create 2 dimensional array in function and use outside of function ?

This is a discussion on How do I create 2 dimensional array in function and use outside of function ? within the C Programming forums, part of the General Programming Boards category; I have a program to re-write for a small amount of extra credit, but have not been able to find ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Angry How do I create 2 dimensional array in function and use outside of function ?

    I have a program to re-write for a small amount of extra credit, but have not been able to find examples/info in books or on the web.
    Originally the program involved three arrays with 2 dimensions each. Elements of arrays 1 and 2 were added together to create the elements for array three. That went okay I think, I got the desired results etc.
    Now I need to create two functions to be called from main, that will create/fill the arrays and send back the array data to main to do the calculations for the third array.
    I thought my function prototype, call etc was set up properly, but I either get zero's in everything or a few systax or declaration not terminated properly errors. I have not been able to locate examples of the right way to set up these functions.
    Don't laugh, but this is one of many variations I have tried:
    Prototype
    int even();
    Call
    int even ()
    Body
    int(arrE[2][4])even()

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Erm, I think the arrays stay in main, and you pass these arrays (as pointers) to your functions (which will update the arrays in main, because arrays are passed as pointers)

    For a [2][4] array, it goes like this
    Code:
    void init ( int arr[2][4] );
    
    int main ( ) {
        int foo[2][4];
        init( foo );
        return 0;
    }
    
    void init ( int arr[2][4] ) {
        // do stuff
    }
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    I think I don't understand arrays in functions

    This is what the mess looks like
    /* A Program to populate 2 arrays, one with even numbers and one with odd numbers
    then use an empty array to store the totals of the numbers in the two original
    arrays. Print/display the contents of all three arrays.
    Populate the arrays by using functions
    Version 2, Function Usage */

    #include <stdio.h>

    void even(int arrE[2][4]); /*Function Prototype*/
    void odd(int arrO[2][4]); /*Function Prototype*/
    int arrE [2][4];/*Array Variable*/
    int arrO [2][4];/*Array Variable*/
    int arrTotal[2][4]; /*intialize the array to hold the totals of the other arrays*/
    int r,c;

    int main ()
    {

    even (int arrE[2][4]);
    odd (int arrO[2][4]);
    for (r=0;r<2;++r) /*for loop, for beginning row, max rows and increment*/
    {
    for (c=0;c<4;++c) /*for loop total #'s, for beginning column, max columns, and increment*/
    {
    arrTotal[r][c]=(arrE[r][c]+arrO[r][c]); /*formula to add elements of two arrays
    and place in elements of total array*/
    printf("\nThese are the totals of the elements : %d plus %d equals %d",
    arrE[r][c],arrO[r][c],arrTotal[r][c]); /*Display results*/
    }
    }
    printf("\n\nThe program has ended !!!"); /*Message*/

    return;
    }

    void even(int arrE[2][4])
    {

    int arrE [2][4]={{2,4,6,8},{10,12,14,16}};/*Populate the even # array*/

    for (r=0; r<2; ++r) /*for loop even #'s, for beginning row, max rows and increment*/
    {
    for (c=0;c<4;++c) /*for loop, for beginning column, max columns, and increment*/
    {
    printf("\t%d ",(arrE[r][c])); /*display array elements*/
    }
    }
    return ();
    }

    void odd(int arrE[2][4])
    {
    int arrO [2][4]={{1,3,5,7},{9,11,13,15}}; /*Populate the odd # array*/
    for (r=0; r<2; ++r) /*for loop even #'s, for beginning row, max rows and increment*/
    {
    for (c=0;c<4;++c) /*for loop, for beginning column, max columns, and increment*/
    {
    printf("\t%d ",(arrE[r][c])); /*display array elements*/
    }
    }
    return ();
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Getting close...

    > even (int arrE[2][4]);
    > odd (int arrO[2][4]);
    When calling functions, its just the array name
    even ( arrE );
    odd ( arrO );

    And your even function should look like this
    Code:
    void even(int arrE[2][4]) {
        int temp [2][4]={{2,4,6,8},{10,12,14,16}};/*Populate the even # array*/
        int r, c;
        for (r=0; r<2; ++r)  /*for loop even #'s, for beginning row, max rows and increment*/
        {
            for (c=0;c<4;++c) /*for loop, for beginning column, max columns, and increment*/
            {
                arrE[r][c] = temp[r][c];
            }
        }
        return;
    }
    Something similar for odd...
    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.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    whew !!!!!

    thx so much, I shall study the errors in my code.
    A few changes and it Works !!!!!!!!

Popular pages Recent additions subscribe to a feed

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