I read that to declare a function with a bidimensional array as parameter, I need to specify the number of columns of the array, but if I don't know the size of the matrix I need to use double pointer. However, I can't understand this process very well, can someone give me some short and simple example of this type of function?
Thank you
Also, I tried to write a program but it gives me some warning.
#include<stdio.h>
int diagonal(int**,int );--->first note
int main(){
int N;
scanf("%d",&N);
int array[N][N];
int i;
int j;
printf("Insert the numbers:\n");
for(i=0;i<N;i++){
for(j=0;j<N;j++){
scanf("%d",&array[i][i]);
}
}
diagonal(&array[N][N],N); ---->second warning
}
int diagonal(int**A,int N){
int i;
int condition=0;
for(i=0;i<N-1;i++){
if(A[0][0]!=A[i+1][i+1]){
return -1;
}else{
condition=1;
}
}
if(condition==1){
int val=A[i][i];
int sum= N*val;
return sum;
}
}
Compiler output:
3|note: expected 'int **' but argument is of type 'int *'|
17|warning: passing argument 1 of 'diagonal' from incompatible pointer type|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
The program should scan a matrix and return the sum of elements on the main diagonal if the elements on the diagonal have the same value return -1 if not.