2

Respected members of stackoverflow, Am a complete armature to c program,I want to access the element of the matrix using pointers.like i want to print the elements of the matrix using point.I have attached the code along with the incorrect output.please help me .thank you

`
#include<stdio.h>
#include<conio.h>

void print(int **arr, int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
      for (j = 0; j < n; j++)
      {
    printf("%d ",*(arr+i*n+j));                   //print the elements of the matrix
    //printf("%d ", *((arr+i*n) + j));
      }
      printf("\n");
    }
}


int main()
{
    int arr[20][20],m,n,c,d;
    clrscr();
    printf("row=\n");
    scanf("%d",&m);
    printf("col=\n");
    scanf("%d",&n);
    for(c=0;c<m;c++)
    {
     for(d=0;d<n;d++)
      {
       scanf("%d",&arr[c][d]);
      }
    }
    for(c=0;c<m;c++)                      //print the matrix without function calling
    {
     for(d=0;d<n;d++)
     {
     printf("%d ",arr[c][d]);
     }
     printf("\n");
    }
    print((int **)arr, m, n);            //print the matrix using function calling
    getch();
    return 0;
}` 

above code produce the output shown below

row=
2
col=
3
//elements of the matrix
2
3
4
5
6
7
//print without using function
2 3 4
5 6 7
//print using function"print(int **a,int m,int n)"
2 3 4
0 0 0

when using function am not getting the exact matrix value. NOTE:print(int **a,int m,int n) should be used.

vignesh
  • 29
  • 1
  • 2
  • 2
    `int arr[20][20]` is *not* `int**`, and the compiler failing unless you forcibly make that cast should be an indicator that something is wrong. – WhozCraig May 29 '15 at 19:09
  • @WhozCraig:Thanks you for your reply.function is working but the elements are not printed in correct order.please help me. – vignesh May 29 '15 at 19:17
  • Odd definition of "working". You're violating aliasing. A pointer to pointer to `int`, `int**`, is *not* synonymous to a pointer to `int[20]`, i.e. `int (*)[20]`, which is what you're argument expresses to be. Removing the cast and attempting to compile should tell you in-as-much the same thing I just did. A cast isn't the solution to "fix" that error. – WhozCraig May 29 '15 at 19:20
  • `printf("%d ",*(arr+i*n+j));` change to `printf("%d ", arr[i][j]);` (for maskacovnik's answer) – BLUEPIXY May 29 '15 at 19:24

2 Answers2

4

As @WhozCraig told in his coment, the arr should be int**

int** arr = malloc(sizeof(int*)*m);
int a;
for(a=0;a<m;a++)
    arr[a]=malloc(sizeof(int)*n);
for(c=0;c<m;c++)
{
 for(d=0;d<n;d++)
  {
   scanf("%d", &arr[c][d]); //scanf("%d",arr+c*n+d);
  }
}

Note: not tested
Edit: malloc not casted

BLUEPIXY
  • 38,201
  • 6
  • 29
  • 68
maskacovnik
  • 3,009
  • 5
  • 18
  • 26
3

If your compiler supports C99 then try the following

#include<stdio.h>
#include<conio.h>

#define N 20

void print(  int m, int n, int ( *a )[n] )
{
    for ( int ( *p )[n] = a; p != a + m; ++p )
    {
        for ( int *q = *p; q != *p + n; ++q ) printf( "%d ", *q );
        printf( "\n" );
    }
}

int main( void )
{
    clrscr();

    int m, n;

    printf( "row = " );
    scanf( "%d", &m );
    printf( "col = " );
    scanf( "%d", &n );

    if ( m > N ) m = N;
    if ( n > N ) n = N;

    int arr[m][n];

    for ( int i = 0; i < m; i++ )
    {
        for ( int j = 0; j < n; j++ )
        {
            scanf( "%d", &arr[i][j] );
        }
    }

    for ( int i = 0; i < m; i++ )
    {
        for ( int j = 0; j < n; j++ ) printf( "%d ", arr[i][j] ); 
        printf( "\n" );
    }


    print( m, n, arr );

    getch();

    return 0;
}` 

Otherwise define the function like

void print(  int m, int n, int ( *a )[N] )
{
    int ( *p )[N];
    int *q;

    for ( p = a; p != a + m; ++p )
    {
        for ( q = *p; q != *p + n; ++q ) printf( "%d ", *q );
        printf( "\n" );
    }
}
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268