Hey,
I wanted to print the values of a array from a function by passing the array as well as the number of elements to be read.
For a single dimensional array, this is how i have written it. It's pretty straight forward. I want to read 5 elements from the 5th element in the array.
Code:
#include<stdio.h>
void display(int array[],int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d ",array[i]);
}
}
void main()
{
int array[]= {1,2,3,4,5,6,7,8,9,10};
display(&array[4],5);
}
But, when i try the same for a multi-dimensional array. I am not able to follow the same approach.
Code:
#include<stdio.h>
void display(int array[][10],int size)
{
int i,j;
for(i=0;i<1;i++)
{
for(j=0;j<size;j++)
{
printf("%d\n",array[i][j]);
}
}
}
void main()
{
int array[2][10]={ {1,2,3,4,5,6,7,8,9,10},
{11,12,13,14,15,16,17,18,19,20}
};
display(&array[0][4],5);
}
With this code I want to print the five elements from the element present in [0][4].
But shows an error that
Code:
D:\Bennet\Codeblocks C\Learning C\SingleDimentionalArray.c||In function 'main':|
D:\Bennet\Codeblocks C\Learning C\SingleDimentionalArray.c|18|warning: passing argument 1 of 'display' from incompatible pointer type [enabled by default]|
D:\Bennet\Codeblocks C\Learning C\SingleDimentionalArray.c|2|note: expected 'int (*)[10]' but argument is of type 'int *'|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case.
But can't figure out a way to achieve this, Can you explain how this works for mult- dimensional array's?