I'm following a tutorial on how to make an operating system in C. Everything works in the tutorial, no error messages, nothing, while I get the error specified in the title. Is making variables from the struct "Char" global even an option?
#include "print.h"
const static size_t NUM_COLS = 80;
const static size_t NUM_ROWS = 25;
struct Char {
uint8_t character;
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) {
character: ' ',
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 == '\n') {
print_newline();
return;
}
if (col > NUM_COLS) {
print_newline();
}
buffer[col NUM_COLS * row] = (struct Char) {
character: (uint8_t) character,
color: color,
};
col ;
}
void print_str(char* str) {
for (size_t i = 0; 1; i ) {
char character = (uint8_t) str[i];
if (character == '\0') {
return;
}
print_char(character);
}
}
void print_set_color(uint8_t foreground, uint8_t background) {
color = foreground (background << 4);
}
I am fairly new to C, so I might not know even easier things than this. Thanks in advance!
I also apologize if this question has been asked already, I couldn't find the answer anywhere.
CodePudding user response:
struct Char empty = (struct Char) {
character: ' ',
color: color,
};
That is not how you initialize a struct
object in C. You need to do something like
struct Char empty = { ' ', color };
or
struct Char empty = { .character = ' ', .color = color };
or
struct Char empty;
empty.character = ' ';
empty.color = color;
Also, C doesn't like trailing commas in initializers or enumerations - the compiler will complain about a missing expression in that case.
CodePudding user response:
For making a struct
variable/object global in C:
//m.c - the content of the global struct variable will be printed in this translational unit
#include<stdio.h>
struct node
{
int i;
char c;
};
extern struct node no;
void fun();
int main()
{
fun();
printf("i = %d, c= %c\n",no.i,no.c);
return 0;
}
in the other translational unit where the global struct
variable is declared and defined:
//n.c
struct node
{
int i;
char c;
}no;
void fun()
{
no.i=89;
no.c='S';
}
The struct
needs to be present in both the translational units, so it is better to declare that in a header and then include that header wherever required.