我正在學習一個關于如何用C語言制作作業系統的教程。教程中的所有內容都能運行,沒有錯誤資訊,什么都沒有,而我卻得到了標題中指定的錯誤。讓結構 "Char "中的變數成為全域變數是否可行?
#include "print.h"
const static size_t NUM_COLS = 80;
const static size_t NUM_ROWS =25;
struct Char {
uint8_t字符。
uint8_t color。
};
struct Char* buffer = (struct Char*) 0xb8000;
size_t col = 0;
size_t row = 0;
uint8_t color = PRINT_COLOR_WHITE | PRINT_COLOR_BLACK << 4;
void clear_row(size_t row) {
struct Char empty =(struct Char){
字符。' '/span>,
color: color,
};
for (size_t col = 0; col < NUM_COLS; col ) {
buffer[col NUM_COLS * row] = empty;
}
}
void print_clear() {
for (size_t i = 0; i < NUM_ROWS; i ) {
clear_row(i);
}
}
void print_newline() {
col = 0;
if (row < NUM_ROWS - 1) {
row ;
return;
}
for (size_t row = 1; row < NUM_ROWS; row ) {
for (size_t col = 0; col < NUM_COLS; col ) {
struct Char character = buffer[col NUM_COLS * row] 。
buffer[col NUM_COLS * (row - 1) ] = character;
}
}
clear_row(NUM_COLS - 1) 。
}
void print_char(char character) {
if (character == '
') {
print_newline()。
return;
}
if (col > NUM_COLS) {
print_newline()。
}
buffer[col NUM_COLS * row] = (struct Char) {
字符。(uint8_t) character,
color: 顏色。
};
col ;
}
void print_str(char* str) {
for (size_t i = 0; 1; i ) {
char character = (uint8_t) str[i];
if (character == ' ') {
return;
}
print_char(character)。
}
}
void print_set_color(uint8_t foreground, uint8_t background) {
color = foreground (background << 4) 。
我對C語言相當陌生,所以我可能不知道比這更容易的事情。提前感謝!
如果這個問題已經被問過,我也很抱歉,我在任何地方都找不到答案。
uj5u.com熱心網友回復:
struct Char empty = (struct Char) {
字符。' '/span>,
color: color,
};
這不是你在C語言中初始化一個struct物件的方法,你需要做的是
struct Char empty = { ' ' , color }。
或者
struct Char empty = { . character = ' '/span>, .color = color };
或者
struct Char empty;
empty.character = ' '。
empty.color = color。
另外,C語言不喜歡初始化器或列舉中的尾部逗號--在這種情況下,編譯器會抱怨運算式丟失。
uj5u.com熱心網友回復:
對于在C語言中使一個結構變數/物件成為全域變數:
/m.c - 全域結構變數的內容將被列印在這個翻譯單元中。
#include<stdio.h>/span>
struct node
{
int i;
char c;
};
extern struct node no;
void fun();
int main(); int main()
{
fun()。
printf("i = %d, c= %c
",no.i,no.c)。
return 0。
在另一個翻譯單元中,全域struct變數被宣告和定義:
//n.c。
struct node
{
int i;
char c;
}no;
void fun()
{
no.i=89。
no.c='S'。
}
struct需要出現在兩個翻譯單元中,所以最好在一個頭中宣告,然后在需要的地方包括該頭。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/306960.html
標籤:
