pass 2d array to function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bharat64
    New Member
    • Oct 2007
    • 1

    pass 2d array to function

    How to Pass 2d Array in to function of c++

    I have two 2d array of 3 X 3 named a and b in void main function

    Passing both to mult function and return back to main function with the product of a and b ...How?
    Last edited by bharat64; Oct 16 '07, 12:20 PM. Reason: more clarity
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by bharat64
    How to Pass 2d Array in to function of c++
    Hi,

    You can directly pass it in function like normal parameter.

    [CODE=CPP]void functionarray (int arg[][], int length)
    {
    ...function body...
    }
    [/CODE]

    Regards

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by zodilla58
      void functionarray (int arg[][], int length)
      {
      ...function body...
      }
      Actually, that won't compile. C++ has only one-dimensional arrays and the number of elements is specified between brackets.

      Read this:
      Originally posted by weaknessforcats
      First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
      [code=c]
      int array[5];
      [/code]

      That is an array of 5 elements each of which is an int.

      [code=c]
      int array[];
      [/code]

      won't compile. You need to declare the number of elements.

      Second, this array:
      [code=c]
      int array[5][10];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 int.

      [code=c]
      int array[5][10][15];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


      [code=c]
      int array[][10];
      [/code]

      won't compile. You need to declare the number of elements.

      Third, the name of an array is the address of element 0
      [code=c]
      int array[5];
      [/code]

      Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

      [code=c]
      int array[5][10];
      [/code]

      Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
      [code=c]
      int array[5][10];

      int (*ptr)[10] = array;
      [/code]

      Fourth, when the number of elements is not known at compile time, you create the array dynamically:

      [code=c]
      int* array = new int[value];
      int (*ptr)[10] = new int[value][10];
      int (*ptr)[10][15] = new int[value][10][15];
      [/code]

      In each case value is the number of elements. Any other brackets only describe the elements.

      Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

      [code=c]
      int** ptr = new int[value][10]; //ERROR
      [/code]

      new returns the address of an array of 10 int and that isn't the same as an int**.

      Likewise:
      [code=c]
      int*** ptr = new int[value][10][15]; //ERROR
      [/code]

      new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
      With the above in mind you call a funcion by passing in the address of element 0.

      This array:
      [code=cpp]
      int array[5];
      [/code]
      is a passed as:
      [code=cpp]
      functionarray(a rray, 5);
      [/code]
      where:
      [code=cpp]
      void functionarray (int arg* int length)
      {
      ...function body...
      }
      [/code]

      But this array:
      [code=cpp]
      int array[5][3];
      [/code]
      is passed as:
      [code=cpp]
      functionarray(a rray, 5);
      [/code]
      where
      [code=cpp]
      void functionarray (int (*arg)[3] int length)
      {
      ...function body...
      }
      [/code]

      Comment

      • smalpani
        New Member
        • Aug 2007
        • 29

        #4
        [CODE = c]

        void func(int array[][], int row,int col)\\
        {
        /*this is just a proof*/
        }

        int main()
        {

        int a[5][5];
        func(a,5,5);
        return 0;

        }

        [/CODE]

        This compiles without any problem for me

        Comment

        • Meetee
          Recognized Expert Contributor
          • Dec 2006
          • 928

          #5
          Originally posted by weaknessforcats
          Actually, that won't compile. C++ has only one-dimensional arrays and the number of elements is specified between brackets.

          Read this:


          With the above in mind you call a funcion by passing in the address of element 0.

          This array:
          [code=cpp]
          int array[5];
          [/code]
          is a passed as:
          [code=cpp]
          functionarray(a rray, 5);
          [/code]
          where:
          [code=cpp]
          void functionarray (int arg*, int length)
          {
          ...function body...
          }
          [/code]

          But this array:
          [code=cpp]
          int array[5][3];
          [/code]
          is passed as:
          [code=cpp]
          functionarray(a rray, 5);
          [/code]
          where
          [code=cpp]
          void functionarray (int (*arg)[3] int length)
          {
          ...function body...
          }
          [/code]
          But I tried this code for two dimentions. It works correctly.

          [CODE=CPP]
          void printarray(int args[][2],int rows,int cols)
          {
          for(int i=0;i<rows;i++)
          {
          for(int j=0;j<cols;j++)
          cout<<args[i][j]<<endl;
          }
          }
          int main()
          {
          int firstarray[][2] = {{1,2},{3,4}};
          printarray(firs tarray,2,2);
          }
          [/CODE]

          Regards

          Comment

          • smalpani
            New Member
            • Aug 2007
            • 29

            #6
            Even this will work, i did not understand moderator's post

            [CODE=CPP]
            void printarray(int args[][],int rows,int cols)
            {
            for(int i=0;i<rows;i++)
            {
            for(int j=0;j<cols;j++)
            cout<<args[i][j]<<endl;
            }
            }
            int main()
            {
            int firstarray[][2] = {{1,2},{3,4}};
            printarray(firs tarray,2,2);
            }
            [/CODE]

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by smalpani
              Even this will work, i did not understand moderator's post


              Code: ( cpp )
              void printarray(int args[][],int rows,int cols)
              {
              for(int i=0;i<rows;i++)
              {
              for(int j=0;j<cols;j++)
              cout<<args[i][j]<<endl;
              }
              }
              int main()
              {
              int firstarray[][2] = {{1,2},{3,4}};
              printarray(firs tarray,2,2);
              }
              This won't even compile. Here is what Visual Studio.NET had to say:
              [code=c]
              c:\scratch\inst ructor\question 2007-10-17-01.cpp(18) : error C2087: 'args' : missing subscript
              c:\scratch\inst ructor\question 2007-10-17-01.cpp(29) : error C2664: 'printarray' : cannot convert parameter 1 from 'int [2][2]' to 'int [][1]'
              Types pointed to are unrelated; conversion requires reinterpret_cas t, C-style cast or function-style cast
              [/code]

              How can you say this works?

              Comment

              • Meetee
                Recognized Expert Contributor
                • Dec 2006
                • 928

                #8
                Originally posted by weaknessforcats
                This won't even compile. Here is what Visual Studio.NET had to say:
                [code=c]
                c:\scratch\inst ructor\question 2007-10-17-01.cpp(18) : error C2087: 'args' : missing subscript
                c:\scratch\inst ructor\question 2007-10-17-01.cpp(29) : error C2664: 'printarray' : cannot convert parameter 1 from 'int [2][2]' to 'int [][1]'
                Types pointed to are unrelated; conversion requires reinterpret_cas t, C-style cast or function-style cast
                [/code]

                How can you say this works?
                I tried it on linux. and compiles there!! Can it be a platform dependent? Kindly clearify.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by zodilla58
                  I tried it on linux. and compiles there!! Can it be a platform dependent? Kindly clearify.
                  This has nothing to do with Linux.

                  What compiler are you using??

                  Are you using the -pedantic switch to disable non ANSI C extensions?

                  I mean, this is just fundamental array handling in C. It's been documented since 1972.

                  Comment

                  Working...