Reputation: 13
I'm beginner in C and I don't understand why when i run the code below
#include <stdio.h>
#include <stdlib.h>
#define DIMMAX 5
#define MAXSTRINGA 20
typedef struct{
int num;
int den;
} Razionale;
typedef Razionale Matrice[DIMMAX][DIMMAX];
void aquisisciRazionale(Razionale);
void acquisisciMatrice(Matrice[DIMMAX][DIMMAX]);
void aquisisciRazionale(Razionale n){
printf("Inserire numeratore: ");
scanf("%d", &n.num);
printf("Inserire denominatore: ");
scanf("%d", &n.den);
}
void acquisisciMatrice(Matrice mat[DIMMAX][DIMMAX]){
int i, j;
for(i=0; i<DIMMAX; i++){
for(j=0; j<DIMMAX; j++){
aquisisciRazionale(mat[i][j]);
}
}
}
int main(int argc, char *argv[]) {
Matrice m[DIMMAX][DIMMAX];
acquisisciMatrice(m);
}
I have this error:
[Error] incompatible type for argument 1 of 'aquisisciRazionale'
[Note] expected 'Razionale' but argument is of type 'struct Razionale (*)[5]'
I don't understand if there is an error when I define structures or when I define functions. Can someone help me?
Upvotes: 1
Views: 1318
Reputation: 781028
The Matrice
typedef is already a 2-dimensional array. When you declare your function
void acquisisciMatrice(Matrice mat[DIMMAX][DIMMAX]){
you're saying that mat
is a 2-dimensional array of Matrice
, which is a 4-dimensional array of Razionale
. mat[i][j]
is then a Matrice
, not a Razionale
.
The function should just be
void acquisisciMatrice(Matrice mat){
since the dimensions are part of the Matrice
typedef. You should also change the variable in main()
:
Matrice m;
acquisisciMatrice(m);
Note that acquisisciRazionale()
will not fill in the matrix elements in acquisisciMatrice()
. When you call
acquisisciRazionale(mat[i][j]);
you're passing a copy of the array element. scanf()
is then writing into that copy, which has no effect on the original array. You should pass a pointer.
acquisisciRazionale(&mat[i][j]);
and change the function to:
void aquisisciRazionale(Razionale *n){
printf("Inserire numeratore: ");
scanf("%d", &(n->num));
printf("Inserire denominatore: ");
scanf("%d", &(n->den));
}
Upvotes: 2