Thread: passing uninitialized 2d array to function

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    18

    passing uninitialized 2d array to function

    Hi,

    The below program does some operations on multiple matrices. For reading these multiple matrices, am using a function readmat().

    Code:
    #include<stdio.h>
    void readmat(int *a)
    {
        int i,j;
        for(i=0;i<2;i++)
        for(j=0;j<2;j++)
            scanf("%d",a[i][j]);
    }
    
    void printmat(int *a)
    {
        int i,j;
        for(i=0;i<2;i++)
        {
            for(j=0;j<2;j++)
            printf("\t%d",*a[i][j]);
            printf("\n");
        }
    }
    
    int main()
    {
    
        int a[2][2], b[2][2], c[2][2];
        printf("\n Enter Matrix A:\n");
        readmat(&a);
        printf("\n Enter Matrix B:\n");
        readmat(&b]);
        printf("\n\n Matrix A: \n");
        printmat(&a);
        printf("\n\m Matrix B: \n");
        printmat(&b);
        return 0;
    }
    This program compiles well. But fails during runtime after reading 1st element of the array. I guess, the problem is here with passing the uninitialized 2d array to function by reference. Can someone show me how to do this?
    -
    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    int readmat(int a [][2]) ;
    or
    int readmat( int (*a)[2]);

    c-faq

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This program compiles well.
    I really can't believe this. a[i][j] should be absolutely meaningless when you have int *a. At the very least, your compiler should issue a diagnostic for it; and I would expect an error rather than a warning, because I don't see how a compiler could even try to interpret it.

    The call readmat(&b]) certainly should bring about an error from your compiler. You really should copy and paste code, and not just re-type it. You've got a good chance of introducing errors if you re-type it.

    Beyond that, all other calls to readmat() and printmat() require diagnostics for invalid pointer conversions.

    In short, any compiler that builds this code with no diagnostics is no good and should be scrapped immediately.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    18
    The call readmat(&b]) certainly should bring about an error from your compiler. You really should copy and paste code, and not just re-type it. You've got a good chance of introducing errors if you re-type it.

    Am sorry, While copying, I think I didn't copy the code which was compiling. Without the modification, i.e. "]" it was compiling well. I'm using codeblocks with GCC on Fedora 11.

    I followed the steps provided by Naung and got the program working

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  5. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 03:32 PM