Thread: prinitng 2D array form pointer ????

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    prinitng 2D array form pointer ????

    Ok so here's my problem... I know this code works for a pointer of a 2Darray inside the same function, but when I try to do the same for a 2D array pointer passed by reference it obviously won't work... any help??

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    the | after the < sign in the for loop is just my cursor, ignore.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Can you just copy the code instead of posting a screenshot, I can hardly read that crap since it's so dark.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Code:
    /************************************************************************************
     print_matrix():
     This will print the contents of the matrix M which has r rows and c  
     columns.  The matrix will be printed one row per line.  Each row will  
     be printed between pipe characters('|').  Each element of the vector  
     will have a field width of 8 and each element will be left aligned  
     within that field. 
     ************************************************************************************/
    void print_matrix(int *M, int r, int c)
    {
    	
    	int (*p)[c], i;
    	
    	for ( p = M; p < &M[r]; p++ )
    	{
    		printf("\n|");
    		for ( i = 0; i < c; ++i)
    		{
    			printf("%d", (*p)[i]);
    			if ( i == (c-1)) printf("|");
    		}
    	}		
    	
    	printf("\n");
    	
    }

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You will have to pass in the 2d array and give its dementions. Your declaration is off.


    Code:
    void print_matrix(int **M, int width, int length)

    There are a few other ways to accomplish this.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Your array traversal logic would be correct if M were an int **. However, since M is an int *, your row "pointer" must increment by c elements each time, assuming row major ordering.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    thanks. I solved it with this:

    Code:
    void print_matrix(int *M, int r, int c)
    {
    	
    	int i, j, k=0;
    	
    	for ( i = 0; i < r; ++i )
    	{
    		printf("\n|");
    		for ( j = 0; j < c; ++j)
    		{
    			printf("%d\t", M[k++]);
    			if ( j == (c-1)) printf("|");
    		}
    	}		
    	
    	printf("\n");
    	
    }
    the output is ........ty, but that's what my professor wants.. i guess i could improve it without it hurting my grade but i ahve to turn it in in an hour and i have one more method to work on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D pointer to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 2
    Last Post: 07-20-2009, 08:58 AM
  2. 2D array pointer
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-30-2008, 12:30 PM
  3. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  4. Need to pass a pointer of a unknown sized 2d array
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2007, 10:15 PM
  5. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM