gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:58:19 32 4
gpt4 key购买 nike

编辑:已解决。 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com