Thread: Problems with pointer to multidimensional array

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Problems with pointer to multidimensional array

    I'm having some problems iterating through a bidimensional array,as I cn't print all the elements on a matrix form.Here's my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #define L1 3
    #define C1 2
    #define L2 4
    #define C2 3
    
    void escreve1(int mat[][C1],int lin, int col);
    void escreve2(int mat[][C2],int lin, int col);
    
    void main()
    {
    	int mat1[L1][C1]={{1,2},{3,4},{5,6}};
        int mat2[L2][C2]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
    
        printf("\nMatriz mat1:\n");
        escreve1(mat1, L1, C1);
       printf("\nMatriz mat2:\n");
        escreve2(mat2, L2, C2);
    
    
    }
    void escreve1(int mat[][C1] , int lin, int col)
    {
     int  *z;
    
    	   for(z=mat[0];z<mat[0]+col*lin;z++)
    	  {
    	   
    	   printf("&#37;d\t",*z );
    	   if(z==mat[0]+(col-1))
    		  printf("\n");
    	   
    	  }
    	
      
       
       
    }
    
    void escreve2(int mat[][C2] , int lin, int col)
    {
    
    
     int *z;
    
       
    	  for(z=mat[0];z<mat[0]+col*lin;z++)
    	  {
    	    printf("%d\t",*z );
    	   if(z==mat[0]+(col-1))
    		  printf("\n");
    	  }
    I want the output to be something like this:

    Code:
    Matriz mat1:
       1 2
       3 4
       5 6
    
      Matriz mat2:
       1 2 3
       4 5 6
       7 8 9
       10 11 12
    Any help is very appreciated!

    PS:The output I'm getting is:

    Code:
    Matriz mat1:
       1 2
       3 4 5 6
    
      Matriz mat2:
       1 2 3
       4 5 6 7 8 9 10 11 12
    Last edited by esmeco; 03-08-2008 at 07:19 PM. Reason: forgot to add something

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's easier if you don't use only pointers, then you can use &#37; with the width.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Not that I want to,but I must use only pointers!

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I suppose you might get away with a ptrdiff, something like
    Code:
    /* caveat: http://c-faq.com/aryptr/ary2dfunc2.html */
    void escreve1(int *mat, int lin, int col)
    {
       int *z = mat;
       while ( z < &mat[lin * col] )
       {
          printf("%d%c", *z, (z - mat) % col == col - 1 ? '\n' : '\t' );
          ++z;
       }
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. 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
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM