1

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.

3 Answers 3

2

If you want to use a variable size array as an argument, you need to specify the size first, then use that parameter as the array size in the array argument:

int diagonal(int N, int A[N][N] );

int main() {
    ...
    diagonal(N, array);
}

int diagonal(int N, int A[N][N]){
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry if the question may sounds dumb,but i'm a beginner.In the declaration shoudn't i delete the "N" parameters? i mean if i put int diagonal(int N, int A[N][N] ); the compiler says that "N" is undeclared and if i delete "N" the program won't run
@GiorgioM. In most cases, the parameter names in a declaration are not required, but in this case it's needed to let the compiler know it's dealing with a variable sized array and what specifies the size. Attempting to declare the array parameter as int [][] is an incomplete declaration.
Ahhh nevermind now it works . Thank you so much and thank you to all the people that answered
1

You are passing your array wrong. You should pass array instead of &array[N][N]. When you do &array[N][N] you just get address of [N][N] array's element and pass it, it is invalid address.

diagonal(array, N);

Also, when you do scanf, you, probably wanted to get [i][j] element, but you do [i][i] two times.

scanf("%d", &array[i][j]);

Comments

1

17|warning: passing argument 1 of 'diagonal' from incompatible pointer type|

diagonal(&array[N][N],N); //---->warning

here &array[N][N] is of the type int* but your function definition:

int diagonal(int**,int );

has first argument type as int** so you are sending wrong type as an argument. that's the reason why you are get this warning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.