小编Ape*_*123的帖子

数组被"清零"的困惑

#include<stdio.h>
#include<stdlib.h>

static char mem[4];

int main()
{
    int* A = (int*)(mem);

    mem[0] = 0;
    mem[1] = 1;
    mem[2] = 2;
    mem[3] = 3;

    int i;
    A[0] = 5;

    for (i = 0; i<4; i++)
    {
        printf("%d\t", mem[i]);
    }

    printf("\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么打印5 0 0 0而不是5 1 2 3?为什么阵列"消失了?"

c arrays

1
推荐指数
1
解决办法
108
查看次数

了解程序的输出

我正在看这个堆栈交换问题:如何定期自动调用函数?

我尝试在第一个答案中运行代码

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

void timer_handler (int signum)
{
 static int count = 0;
 printf ("timer expired %d times\n", ++count);
}

int main ()
{
 struct sigaction sa;
 struct itimerval timer;

 /* Install timer_handler as the signal handler for SIGVTALRM. */
 memset (&sa, 0, sizeof (sa));
 sa.sa_handler = &timer_handler;
 sigaction (SIGVTALRM, &sa, NULL);

 /* Configure the timer to expire after 250 msec... */
 timer.it_value.tv_sec = 0;
 timer.it_value.tv_usec = 250000;
 /* ... and every …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
69
查看次数

标签 统计

c ×2

arrays ×1