我试图计算下面给出的程序的执行时间,但它给了我一些错误
#include<stdio.h>
#include<time.h>
int main()
{
clock_t start, end;
start = clock();
int a[5] ={1,2,3,4,51};
int b[5] = {2,3,4,5,6};
int c[5] = {3,4,5,6,7};
for (int i =0;i<5;i++)
{
if(a[i]== NULL )
printf("%d element null::",a[i]);
else
printf("no null values in first array\n");
if(b[i]== NULL )
printf("%d element null::",b[i]);
else
printf("no null values in second array\n");
if(c[i]== NULL )
printf("%d element null::",c[i]);
else
printf("no null values in third array\n");
}
end = clock();
printf("Start time: %i, End time: %i \n" start, end);
return 0;
}
以下是错误和警告..
[eshwar@localhost ~]$ gcc -std=c99 ab.c
ab.c: In function ‘main’:
ab.c:18:9: warning: comparison between pointer and integer [enabled by default]
if(a[i]== NULL )
^
ab.c:24:16: warning: comparison between pointer and integer [enabled by default]
if(b[i]== NULL )
^
ab.c:30:16: warning: comparison between pointer and integer [enabled by default]
if(c[i]== NULL )
^
ab.c:39:44: error: expected ‘)’ before ‘start’
printf("Start time: %i, End time: %i \n" start, end);
为什么会给出这个警告...因为我认为我们可以像这样初始化数组?
答案 0 :(得分:2)
我想你只是想知道数组中的一个数字是否为0。 在这种情况下,您只需将if(a [i] == NULL)更改为if(a [i] == 0)。(与其他两个相同)
正如酷家伙已经说过,字符串和开头之间必须有一个逗号。
该程序将工作,但编译器仍然会抱怨&#39; clock_t&#39;之间的差异。和&#39; int&#39;
所以最后你可以将这一行改为:
printf("Start time: %i, End time: %i \n", (int)start, (int)end);