首先:我会从阅读所有警告开始,我猜它们有很多,从checkme 开始,尽管声明了类型 (void*),但没有返回值——你可能不明白星号的含义。在函数声明中它意味着它返回 void 指针(没有指定的数据类型)并且在调用中它会被取消引用,但是编译器如何允许你这样做,我不知道。
第二:下次正确格式化您的代码。难怪你不能得到它,这是一场灾难。尤其是任何合理的编辑器都可以自动完成。
checkme 肯定有问题:for(p=m;p-m<C;p++){ — 看起来很可笑。 C 名称具有误导性,因为它是行数而不是相反的。顺便说一句,你为什么省略最后 3 列?
if(isEven(&p)==1) — 我不知道它不会崩溃。 & 返回指针,因此当它打算传递一行时,您将指针传递给矩阵。
然后,即使您似乎再次检查整个矩阵。无论如何,最好回到你理解的东西。哦,值应该是双精度值,而不是整数。
gcc -Wall -pedantic -Werror yourcode.c #compile with strict rules
/tmp/x.c: In function ‘ckeckme’:
/tmp/x.c:8:8: error: implicit declaration of function ‘isEven’ [-Werror=implicit-function-declaration]
if(isEven(&p)==1)
^~~~~~
/tmp/x.c:9:5: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration]
printf("%d",p-m);
^~~~~~
/tmp/x.c:9:5: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror]
/tmp/x.c:9:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
/tmp/x.c:9:14: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Werror=format=]
printf("%d",p-m);
~^ ~~~
%ld
/tmp/x.c:5:15: error: unused variable ‘i’ [-Werror=unused-variable]
int **p,j,i;
^
/tmp/x.c:5:13: error: unused variable ‘j’ [-Werror=unused-variable]
int **p,j,i;
^
/tmp/x.c: In function ‘isEven’:
/tmp/x.c:15:22: error: unused variable ‘cc’ [-Werror=unused-variable]
int *ptr,conut=0,cc=0;
^~
/tmp/x.c:15:10: error: unused variable ‘ptr’ [-Werror=unused-variable]
int *ptr,conut=0,cc=0;
^~~
/tmp/x.c: In function ‘main’:
/tmp/x.c:42:14: error: passing argument 1 of ‘ckeckme’ from incompatible pointer type [-Werror=incompatible-pointer-types]
*ckeckme(matrix);
^~~~~~
/tmp/x.c:3:7: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
void *ckeckme(int **m)
^~~~~~~
/tmp/x.c:42:5: error: dereferencing ‘void *’ pointer [-Werror]
*ckeckme(matrix);
^~~~~~~~~~~~~~~~
/tmp/x.c:34:9: error: unused variable ‘j’ [-Werror=unused-variable]
int x,i,j,matrix[R][C]={ {8,1,2},
^
/tmp/x.c:34:7: error: unused variable ‘i’ [-Werror=unused-variable]
int x,i,j,matrix[R][C]={ {8,1,2},
^
/tmp/x.c:34:5: error: unused variable ‘x’ [-Werror=unused-variable]
int x,i,j,matrix[R][C]={ {8,1,2},
^
/tmp/x.c: In function ‘ckeckme’:
/tmp/x.c:11:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
/tmp/x.c: In function ‘isEven’:
/tmp/x.c:31:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
我假设您自己无法思考,因此请考虑以下代码:
#include <stdio.h>
#include <math.h>
int main(void) {
double matrix[8][3]={ {8,1,2},
{3,7,5},
{6,2,14},
{13,8,15},
{8,0,2},
{4,50,26},
{2,84,11},
{12,36,9}};
for( int i = 0; i < 8; i++ ) {
int j;
for( j = 0; j < 3; j++ ) {
if( fmod( matrix[i][j], 2 ) != 0 ) // % operator is only for ints
break;
}
if( j == 3 )
printf( "row %d is all even\n", i+1 );
}
return 0;
}
你唯一需要做的就是用指针来做。 ;)