c - 我的全局变量不断重新定义自己

标签 c initialization global

编辑:已解决。 unsigned char 修复了它。

我在使用家庭酿造库时遇到问题,该库应该伪模拟 vga 80x25 终端,以便我可以处理一些错误的操作系统逻辑。每当我调用 s_put_char 时,它都会完全重置标志字符,并重新初始化我的缓冲区。

我的目标是仅在第一次调用 s_put_char 时才调用 s_init()

我的代码:

#include <stdio.h>

//this is so I can start coding logic without having to build a cross-compiler
//the only s_* method that should access by external code is s_put_char(char,int)

//all methods beginning with s_* are referring to the screen output
#define s_rows 25
#define s_columns 80
#define s_buffer_size 2000 //80x25 = 2000

char flags = 0;

//first bit is if the screen is inited or not.
char s_buffer[s_buffer_size]; //80x25

void s_update()
{
        system("cls");
        printf(s_buffer);
}

void s_init()
{
        int index = 0;
        while(index < s_buffer_size)
        {
                //s_buffer[index] = ' ';
                index++;
        }
        flags += 128; //flip first bit
        s_update;
}

void s_put_char(char c, unsigned int index)
{
        if(flags < 128){ s_init(); }
        //checks to see if the first bit is flipped or not.
        //if first bit not flipped, then screen needs to be inited
        s_buffer[index] = c;
        s_update();
}

我的规范:Mingw + Msys,Win 8

最佳答案

s_init()中:

    s_update;

应该是

    s_update();

不应该吗?

此外 - 由于 flags 是一个有符号字符 -

flags < 128 

永远正确。

尝试将flags定义为unsigned char

编辑:

实际上 - 正如 @KeithThompson 所指出的 - char 可能表现为有符号或无符号,具体取决于实现。

关于c - 我的全局变量不断重新定义自己,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380500/

相关文章:

c - 为什么 symlink() 总是打印 symlink failed?

使用函数连接 C 字符串

C++使用new创建带有初始化程序的对象数组

python - 如何全局安装 Python (dev) 依赖项,这样我就不必在每个 venv 中重新安装它们?

Python:如何跨多个类封装一个变量?

c - 使用 glib 时如何打印 guint64 值?

objective-c - 如何释放通过引用传递给 c 代码并且其成员已分配到那里的结构?

c++ - 静态变量被初始化两次

ruby:在没有父类的情况下调用super

swift - 作为全局属性的 Activity 指标