Thread: How to pass 2D-array to funtcion

  1. #1
    zainulmin
    Guest

    How to pass 2D-array to funtcion

    I don't know how to pass 2D-array to function...
    i try this code for 2D but error happend...

    #include <stdio.h>

    int kira(const int nom[][])
    {
    int jumlah;
    int i,j;
    jumlah =0;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    jumlah = jumlah + nom[i][j];
    }
    }
    return jumlah;
    }

    main()
    {
    int total, nom[3][3];
    int i,j;

    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    nom[i][j] = 1;

    total = kira(nom);
    printf("Jumlah = %d\n", total);
    }

    -------------------
    the errors are
    pass.c: In function `kira':
    pass.c:12: arithmetic on pointer to an incomplete type
    pass.c: In function `main':
    pass.c:27: warning: passing arg 1 of `kira' from incompatible pointer type

    I don't how to do..

    please help me....

    thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't know how to pass 2D-array to function...
    You have it right except for one thing, when passing multidimensional arrays to a function, you must specify the size of all but the first dimension:
    Code:
    #include <stdio.h>
    
    int kira(const int nom[][3])
    {
      int jumlah;
      int i,j;
      jumlah =0;
      for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
        {
          jumlah = jumlah + nom[i][j];
        }
      }
      return jumlah;
    }
    
    int main(void)
    {
      int total, nom[3][3];
      int i,j;
      
      for(i=0;i<3;i++)
        for(j=0;j<3;j++)
          nom[i][j] = 1;
        
      total = kira(nom);
      printf("Jumlah = %d\n", total);
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    zainulmin
    Guest
    hi... thanks a lot for replying my message.

    i still have the same problem.. for you know, i'm using gcc compiler and if i compile code tht corrected by you, this warning was out.


    2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type


    then , when i cast it, like this in the caller funtcion,

    total = kira((const int [][])nom);

    this warning out.


    2d1.c:27: cast specifies array type


    .... but , if i compile in windows (using MS Visual C++),,, no errror and output was printed correctly... i dont know wat to do.

    thanks again

  4. #4
    dv007
    Guest
    Originally posted by zainulmin
    hi... thanks a lot for replying my message.

    i still have the same problem.. for you know, i'm using gcc compiler and if i compile code tht corrected by you, this warning was out.


    2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type


    then , when i cast it, like this in the caller funtcion,

    total = kira((const int [][])nom);

    this warning ou
    2d1.c:27: cast specifies array type


    .... but , if i compile in windows (using MS Visual C++),,, no errror and output was printed correctly... i dont know wat to do.

    thanks again [/B]
    To remove error in GCC compiler, you need to remove CONST in the kira()function since you try to modify this array; so change it to:
    int kira(int nom[][3])

    Also you can use pointer parameter as 2D-array
    such as : int kira(int **nom);
    Good luck!
    DV007

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type
    This is very curious.

    If you have these functions
    void foo ( const int b ) { }
    void bar ( const int b[] ) { }
    void baz ( const int b[][3] ) { }

    With these calls
    foo( nom[0][0] );
    bar( nom[0] );
    baz( nom );

    Then it's only the last one which has a problem with the const.

    What's more curious is that compiling the code with the GNU C++ compiler does NOT produce the same warning (no warnings at all)

    Like so
    D:\code>gcc -Wall -x c++ sample.c

    D:\code>gcc -Wall sample.c
    sample.c: In function `main':
    sample.c:34: warning: passing arg 1 of `baz' from incompatible pointer type

    And using the MSVC++ compiler compiling the same code as either C or C++ results in no warnings either.

    ::more digging::
    Mmm, perhaps its this bug?

    > you need to remove CONST in the kira()function since you try to modify this array
    This would work, but the array isn't being modified by the kira() function, so the use of const in this context is correct.

    > int kira(int **nom);
    This is in no way equivalent to int kira ( int nom[][3] );
    Doing this would break the code

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Perhaps it is a problem with the way they're casting it:

    total = kira((const int [][])nom);

    Just a thought...

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    dv007
    Guest
    Originally posted by Salem
    > 2d1.c:27: warning: passing arg 1 of `kira' from incompatible pointer type
    This is very curious.

    If you have these functions
    void foo ( const int b ) { }
    void bar ( const int b[] ) { }
    void baz ( const int b[][3] ) { }

    With these calls
    foo( nom[0][0] );
    bar( nom[0] );
    baz( nom );

    Then it's only the last one which has a problem with the const.

    What's more curious is that compiling the code with the GNU C++ compiler does NOT produce the same warning (no warnings at all)

    Like so
    D:\code>gcc -Wall -x c++ sample.c

    D:\code>gcc -Wall sample.c
    sample.c: In function `main':
    sample.c:34: warning: passing arg 1 of `baz' from incompatible pointer type

    And using the MSVC++ compiler compiling the same code as either C or C++ results in no warnings either.

    ::more digging::
    Mmm, perhaps its this bug?

    > you need to remove CONST in the kira()function since you try to modify this array
    This would work, but the array isn't being modified by the kira() function, so the use of const in this context is correct.

    > int kira(int **nom);
    This is in no way equivalent to int kira ( int nom[][3] );
    Doing this would break the code
    I just wonder why it cause to break the code when passing
    pointer parameter to the function??? with example above:
    Code:
      
        int kira(int **nom)
       {  int i, j;
           int total = 0;
           for (i = 0; i <3; i++)
               for(j=0; j<3; j++)
                    total =total + nom[i][j];
        
           return total;
      }
    I am not C expert, but I guess that passing array as pointer to a
    function is OK and would not BREAK the code.
    Anyone has any idea?

    DV007

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 08:17 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM