Thread: Copying From Single Dimension Array To Multiple Dimension Array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Copying From Single Dimension Array To Multiple Dimension Array

    I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work.

    Code:
    #include <stdio.h>
    #define SIZE 5
    #define SIZE2 5
    #define ROWS 2
    
    void transarr4(int *(*(arry)), int * arry2, int row);		//Pointer Notation (Multiple Dimension Array)
    
    int main(void)
    {
      int arr[SIZE] = {100,200,300,400,500};
      int arr2[ROWS][SIZE2] = {{},{}};
      int *(*(arr3)) = 0;
    
      transarr4(arr3,arr,ROWS);				//Pointer Notation (Multiple Dimension Array)
    
      return 0;
    }
    
    //==================================================================================================== 
    //	Pointer Notation (Multi-Dimension Array)
    //====================================================================================================
    void transarr4(int *(*(arry)), int * arry2, int row)
    	{
    	int num = 0;
    	int r = 0;
    	int c = 0;
    	for(r = 0; r < row; r++)
    		{
    		for(c = 0; c < SIZE2; c++)
    			{
    			//arr[r][c] = arr2[c];
    			*(*(arry)) = arry2[c];
    			printf("%d",*(*(arry + r)+c));
    			printf("\n");
    			}			
    		}
    	
    	}

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
      int arr2[ROWS][SIZE2] = {{},{}};
    Not in standard C. Find your compiler documentation how to enforce standard mode.(say gcc -std99)


    Code:
      int *(*(arr3)) = 0;
    or simply int **arr3; // it's pointer to pointer

    Still not clear how you want to copy.
    I think you better get yourself familiar with pointer and array first.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Multi-dimension arrays, are simply "arrays of arrays". So it's quite simple. Send your copy function the address of the dimension you want to work with.

    Then, it's just like working with any other array.

    For instance, with a 2D array, you'd send it the address to the row that you want to work on (because you're working with just 1 dimension).

    With a 3D array, you'd send the address of the page (the first dimension) of the 2D array, that you wanted to work on), etc.

    You need to set array3 to the right address, not just 0.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM