CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    Sep 2003
    Posts
    280

    how to pass 2-dimension array to function parameter

    I have a question:
    How to pass 2-dimension(or 3-dimension....) array to function?
    Using pointer,array parameter?
    Could someone teach me?
    Thanks!!

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    This is one way but only works for fixed-size array.

    Code:
    void init(int array[10][10])
    {
    	int count=0;
    	for(int i=0;i<10;++i)
    		for(int j=0; j<10;++j)
    			array[i][j] = count++;
    }
    
    int main(void)
    {
    	int num[10][10];
    	init(num);
    	
    	for(int i=0;i<10;++i)
    	{
    		for(int j=0;j<10;++j)
    		{
    			printf("%d ", num[i][j]);
    		}
    		printf("\n");
    	}
    }

  3. #3
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    You can do like this too. Only the last size must be fixed for 2D array.

    void init(int array[][10]);

  4. #4
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    This is the code to use 3 dimensional array of any size. It is almost the same way for any dimensional array bigger than 3D, except the formula for accessing the array inside the function is different.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    void TempFunction(int* m_iArray, int maxx, int maxy, int maxz);
    
    enum
    {
    MAXX = 7,
    MAXY = 8,
    MAXZ = 9
    };
    
    
    int main()
    {
      int ct1,ct2,ct3;
      int iArray[MAXX][MAXY][MAXZ];
    
      TempFunction (&iArray[0][0][0], MAXX, MAXY, MAXZ);
    
      printf("ct1\tct2\tct3\tArray\n");
      for(ct1=0;ct1<MAXX;ct1++)
      {
        for(ct2=0;ct2<MAXY;ct2++)
        {
          for(ct3=0;ct3<MAXZ;ct3++)
          {
            printf("%d\t%d\t%d\t%d\n",ct1,ct2,ct3,iArray[ct1][ct2][ct3]);
          }
        }
      }
      system("pause");
      return 0;
    
    }
    
    
    void TempFunction(int* iArray, int maxx, int maxy, int maxz)
    {
      int x,y,z;
      for(x=0;x<maxx;x++)
      {
        for(y=0;y<maxy;y++)
        {
          for(z=0;z<maxz;z++)
          {
            iArray[((x*maxy)+y)*maxz+z]=2;
          }
        }
      }
      iArray[((3*maxy)+6)*maxz+3]=233;
    }

  5. #5
    Join Date
    Sep 2003
    Posts
    280
    Originally posted by CBasicNet
    You can do like this too. Only the last size must be fixed for 2D array.

    void init(int array[][10]);
    Why the last size must be fixed???

  6. #6
    Join Date
    Sep 2003
    Posts
    280
    Coud I take a pointer type as parameter to pointer an array?

  7. #7
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    673
    Else it wouldn't compile.

  8. #8
    Join Date
    Sep 2003
    Posts
    280
    Originally posted by CBasicNet
    Else it wouldn't compile.
    I know it.
    But I would like to know why compiler cann't compile it.

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Originally posted by Cooker
    I know it.
    But I would like to know why compiler cann't compile it.
    Although the array is declared as multi-dimension, the memory it occupies is actually one-dimensional. The example below shows that.

    For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next column begins.

    Code:
    void init(int array[][10])
    {
    	int count=0;
    	for(int i=0;i<10;++i)
    		for(int j=0; j<10;++j)
    			array[i][j] = count++;
    }
    
    int main(void)
    {
    	int num[10][10];
    	init(num);
    		
    	int *pointer=&num[0][0];
    	for(int i=0;i<10;++i)
    	{
    		for(int j=0;j<10;++j)
    		{
    			printf("%d ", *pointer);
    			++pointer;
    		}
    		printf("\n");
    	}
    
    	return 0;
    }

  10. #10
    Join Date
    Sep 2003
    Posts
    280
    Originally posted by Kheun
    Although the array is declared as multi-dimension, the memory it occupies is actually one-dimensional. The example below shows that.

    For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next column begins.

    Code:
    void init(int array[][10])
    {
    	int count=0;
    	for(int i=0;i<10;++i)
    		for(int j=0; j<10;++j)
    			array[i][j] = count++;
    }
    
    int main(void)
    {
    	int num[10][10];
    	init(num);
    		
    	int *pointer=&num[0][0];
    	for(int i=0;i<10;++i)
    	{
    		for(int j=0;j<10;++j)
    		{
    			printf("%d ", *pointer);
    			++pointer;
    		}
    		printf("\n");
    	}
    
    	return 0;
    }
    "For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next column begins."
    Could u describe that clearly?
    I don't know what you talked.
    THX.

  11. #11
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Sorry I made a mistake in my explanation. It should be
    For this reason, the compiler somehow need to know the length of the row in order to determine the memory location where the next row begins.
    For simplicity, let us only look at 2D array. My example illustrates that multi-dimension array is actually constructed from a single dimension array. When we are using indexing in an array like array[i][j], the actual representation in 1D array is *(array + i*width + j). In this case, the width is 10.

    As the array is created in main() and passed as a parameter to the init(), the compiler need information to tell init() that the received parameter is a 2D array and has a width of 10. In this case, the program can reference to the correct element within the array when you use array[i][j].

    Hope this helps.

  12. #12
    Join Date
    Sep 2003
    Posts
    280
    Originally posted by Kheun
    Sorry I made a mistake in my explanation. It should be


    For simplicity, let us only look at 2D array. My example illustrates that multi-dimension array is actually constructed from a single dimension array. When we are using indexing in an array like array[i][j], the actual representation in 1D array is *(array + i*width + j). In this case, the width is 10.

    As the array is created in main() and passed as a parameter to the init(), the compiler need information to tell init() that the received parameter is a 2D array and has a width of 10. In this case, the program can reference to the correct element within the array when you use array[i][j].

    Hope this helps.
    Oh!
    I understand that.
    THX.

  13. #13
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    Only the last size must be fixed for 2D array.
    in fact, most compilers simply ignore the first one too even if you do specify it.

  14. #14
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    Just felt compelled to butt in here and say: Use a vector.

    typedef std::vector<std::vector<int> > 2D_ARRAY;

    void foo(2D_ARRAY& a2DArray)
    {

    }
    Last edited by STL MAN; September 27th, 2003 at 03:18 AM.
    DEFENDER OF ALL THINGS STL!!!

  15. #15
    Join Date
    Sep 2003
    Posts
    280
    Originally posted by STL MAN
    Just felt compelled to but in here and say: Use a vector.

    typedef std::vector<std::vector<int> > 2D_ARRAY;

    void foo(2D_ARRAY a2DArray)
    {

    }
    Why "vector<std::vector<int> >" is 2 dimension array?
    Could u describe that in detail?

    THX.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured