可能重复:
C中的循环/计时器
我一直在读关于过去3天的计时器,我找不到任何有用的东西,我试图在实例中理解它,有人可以帮我弄清楚如何为下面的程序设置警报.
如何设置一个定时器,以便它发送2个args,一个是数组名称,第二个是要删除的数字,我知道下面的安全无论如何,我只是想了解如何使用带有args的报警来调用一个函数.
请注意,环境是Linux,我也很欣赏与工作C示例的任何链接.
#include<stdio.h>
int delete_from_array(int arg) ;
int main()
{
int a[10000], i, y ;
//how to set timer here for to delete any number in array after half a second
for (y=0; y < 100; y++) {
for (i=0; i<sizeof(a) / sizeof(int); i++)
a[i] = i;
sleep(1);
printf("wake\n");
}
}
int delete_from_array(int arg)
{
int i, a[1000], number_to_delete=0;
//number_to_delete = arg->number;
for (i=0; i<sizeof(a); i++)
if (a[i] == number_to_delete)
a[i] = 0;
printf("deleted\n");
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是我有一个哈希值,它在1秒后有值过期,所以在我将值插入哈希后,我需要创建一个计时器,以便在我们说之后删除该值1秒,如果我在该间隔(1秒)之前从服务器得到响应,那么我从哈希中删除值并删除定时器,几乎就像在tcp中重传一样
有人可以告诉我这里有什么问题吗?
int arr[15][1];
int main()
{
int x;
for(x=0; x<15; x++)
{
arr[x][0] = x;
arr[x][1] = x;
}
int y;
for(y=0; y<15; y++)
{
printf("[%d][0]=%u\t", y, arr[y][0]);
printf("[%d][1]=%u\n", y, arr[y][1]);
}
}
Run Code Online (Sandbox Code Playgroud)
它给了我下面的输出,我无法弄清楚什么是错的,而[0] [0]和[0] [1]的输出应该是0,0等等其余部分?
[0][0]=0 [0][1]=1
[1][0]=1 [1][1]=2
[2][0]=2 [2][1]=3
[3][0]=3 [3][1]=4
[4][0]=4 [4][1]=5
[5][0]=5 [5][1]=6
[6][0]=6 [6][1]=7
[7][0]=7 [7][1]=8
[8][0]=8 [8][1]=9
[9][0]=9 [9][1]=10
[10][0]=10 [10][1]=11
[11][0]=11 [11][1]=12
[12][0]=12 [12][1]=13
[13][0]=13 [13][1]=14
[14][0]=14 [14][1]=14
Run Code Online (Sandbox Code Playgroud)