i'm trying to compile a function to populate a matrix with zeros and ones as part of an adjacency matrix assignment but i keep getting errors when i use the make comma
this is the function that i have in a populateMat.c file.
#include "defs.h"
void PopulateMat(adjmat *mat[N][N]){
printf("\n the matrix size is [%d] x [%d]\n please enter connections between nodes.\n", N,N);
int i;
int j;
for ( i = 0; i < N ;i++){
for ( j = 0; j < N ;j++)
{
printf("\n is there a connection between node %d to node %d ?\n yes - 1 \n no - 0\n", i, j);
scanf("%d", mat[i][j]);
if (!(&mat[i][j]==0 || (int)&mat[i][j]==1)) {
printf("\n%d",mat[i][j]);
printf("wrong indication inserted.\n insert 1 for yes \n 0 for no ");
j--;
}
}
}
}
and this is the error when i run gcc -Wall -ansi -pedantic populateMat.c
populateMat.c: In function ‘PopulateMat’: populateMat.c:13:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[3][3]’ [-Wformat=] scanf("%d", mat[i][j]); ^ populateMat.c:15:24: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int (*)[3][3]’ [-Wformat=] printf("\n%d",mat[i][j]); ^ /usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/crt1.o: In function `_start': (.text+0x18): undefined reference to `main' collect2: error: ld returned 1 exit status
my main.c is
int main()
{
adjmat mat; // defined in defs.h
typedef int adjmat [N][N];
PopulateMat(&mat);
printf("adjacency matrix received");
// הדפסת מטריצת שכנויות
printMat(mat);
adjacency(&mat);
return 0;
}
and Clion is giving this warning about populateMat(&mat) ** incompatible pointer types passing 'adjmat ' (aka 'int ( )[3][3]') to parameter of type 'adjmat ( )[3]' ** i've been trying to figure it out for way to long, help!
Your scanf("%d", mat[i][j]);
: should be like this in for loop, scanf("%d", &mat[i][j]);
Function definition: void PopulateMat(adjmat *mat, N)
If statement code should look like this: if (!(mat[i][j]==0 || mat[i][j]==1))
No need to use & after ||
The typedef
used to create an alias name for other data type. This statement
typedef int adjmat [N][N];
creates adjmat
an alias of int [N][N]
type.
In this
void PopulateMat(adjmat *mat[N][N]){
the type of function parameter mat
is adjmat *mat[N][N]
, that means mat
is of type adjmat *(*)[N]
which is equivalent to int (*(*)[N])[N][N]
.
In main()
, you are passing &mat
as argument to PopulateMat()
. The type of &mat
is adjmat *
which is equivalent int (*)[N][N]
.
The type of argument passed to PopulateMat()
and type of parameter of PopulateMat()
is not matching, hence you are getting incompatible type warning on this statement - PopulateMat(&mat);
.
You should correct the type of PopulateMat()
function parameter mat
. It should be adjmat *
:
void PopulateMat(adjmat *mat) {
....
....
the adjmat *mat
is equivalent to int (*mat)[N][N]
.
With this, you can access the i
th row and j
th column like this (*mat)[i][j]
.
Note that scanf()
expects pointer as argument. So, for taking input to i
th row and j
th column of mat
, do this
scanf("%d", &(*mat)[i][j]);
Instead of passing address of mat
to PopulateMat()
function, you can pass mat
as well because an array decays to pointer (with few exceptions).
So, you can also do
PopulateMat(mat);
and, in this case, the prototype of Populate()
should be
void PopulateMat(adjmat mat);
here the type of mat
is int (*)[N]
(array to pointer decay only happen for inner most dimension).
With this, in the Populate()
function, you can access the i
th row and j
th column of mat
like this mat[i][j]
.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.