Hello Friends I want to pass a Double Dimension Array to a Function using Pointers...

Right Now i am doing this..

# include <stdio.h>
void show(int *,int,int);
int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};
  
  show(a[0],3,3);
  return(0);
}
void show(int *p,int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",*(p+i*col+j));
     printf("\n");
   }
}

Is it correct way to pass a 2 D array?

Recommended Answers

All 8 Replies

Not quite. It seems like you are treating your 2D array as a 1D array and using index conversion from a 2D access pattern to a 1D one. It actually works because, AFAIK, a statically allocated 2D array is a single contiguous memory region. If the a[] was dynamically allocated, though, your code can easily crash.

Using a 1D array as a 2D array is actually a reasonable thing to do in some cases, but in that case you need to declare your original array as:
int a[9] = {1,2,3,4,5,6,7,8,9}
and access it as:
show(a,3,3);

If you want to do it in the most common way, then you declare an "int a[3][3]" and your function has the signature of:
void show(int **p,int row,int col)
and it should access the array by using the standard [][] notation.

I'll be happy to clarify further if you need more help.

well I think you want to say this...

# include <stdio.h>
void show(int **,int,int);
int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};

  show(a,3,3);
  return(0);
}
void show(int **p,int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

But its showing two errors...

Cannot convert 'int[3] *' to 'int * *'

Type mismatch in parameter 1 in call to 'show(int * *,int,int)'

I just want to learn how to pass a two dimensional Array to a Function using Pointer..

Well, I mostly code embedded C, and our compiler treats int[][] and int** equivalently. Just tried via gcc and you're quite correct.

The below will work for a statically allocated array, however, if you already know the array size the other two parameters are redundant.

# include <stdio.h>

void show(int p[3][3],int,int);

int main()
{
  int a[3][3] = {1,2,3,4,5,6,7,8,9};

  show(a,3,3);
  return(0);
}
void show(int p[3][3],int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

As far as I know, you can't pass statically created multidimensional array as an argument to a function. The way you have done it seems like a good way (if not the only one :)). Just be careful and don't do that on a dynamically allocated array, since you cannot assume that all the cells are in the same memory chunk. Then again, if the array is dynamically allocated, you have better ways to do it.

commented: Good answer! +4

Thanks for the suggestion... well now i tried this..

# include <stdio.h>

void show(int (*)[3],int,int);

int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};

  show(a,3,3);
  return(0);
}
void show(int (*p)[3],int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

Is it right way??

commented: Nice & Clear +0
# include <stdio.h>

void show(int (*)[3],int,int);

int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};

/* I would prefer the explicit:
  int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
note the doubled braces
*/
                

  show(a,3,3);
  return(0);
}
void show(int (*p)[3],int row,int col)
/*I would use:
void show(int p[3][3]) 
using the call 
show(a);

Which emphasizes at a glance (to me anyway), that it's a true 2D array
that is being referred to, here.
*/

{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

Is it right way??

Always test it with YOUR compiler and system. If it's always accurate - and you get no warnings (of note), or crashes, or errors in the output - then you can say "I didn't find any bugs, at all!" ;)

Because you can never prove that a program of any complexity, has no bugs. You can only prove that it had no bugs that were found, in your tests.

commented: Detailed and Usefull Information +1

Thanks Friends.. All suggestion are really helpful to me..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.