小编Ben*_*att的帖子

如何在 C 中没有指针的情况下检索超出范围的静态值?

我正在尝试解决Effective C第 2 章中的练习 1 ,它说:

“向清单 2-6 中的计数示例添加检索函数以检索计数器的当前值”

清单 2-6 中的代码是:

#include <stdio.h>

void increment(void) {
    static unsigned int counter;
    counter++;
    printf("%d ", counter);
}

int main(void) {
    for (int i = 0; i < 5; i++) {
        increment();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我尝试了几件事但失败了,我不明白如何检索计数器的值,因为在增量函数之外它超出了范围并且没有可以使用的指针。

c static scope

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

标签 统计

c ×1

scope ×1

static ×1