String literals not working
Posted: Fri Jan 29, 2021 1:48 am
I am pretty new to OS dev, and I am trying to get some text to print out some text. It seems that if I pass a string to a function, it gets turned into nullchars, for example the code below:
```c
#include "../drivers/ports.h"
#include "../drivers/screen.h"
void print_first_char(char* str)
{
print_char(str[0], 0, 10, WHITE_ON_BLACK);
}
void kernel_main()
{
clear_screen();
char* str = "rewqtqrewqre";
print_char(str[0], 0, 10, WHITE_ON_BLACK);
print_first_char(str);
}
```
Will sucsessfuly print the 'r' character when the `print_first_char` function is commented out, however upon uncommenting it it no longer works,
I also found that looping through a string with a for loop does not work, in the following code, the top block works fine, and the bottom one does not.
```c
void kernel_main()
{
clear_screen();
char* str = "rewqtqrewqre";
print_char(str[0], 0, 10, WHITE_ON_BLACK);
print_char(str[1], 1, 10, WHITE_ON_BLACK);
print_char(str[2], 2, 10, WHITE_ON_BLACK);
print_char(str[3], 3, 10, WHITE_ON_BLACK);
print_char(str[4], 4, 10, WHITE_ON_BLACK);
print_char(str[5], 5, 10, WHITE_ON_BLACK);
}
```
and this code does not work:
```c
void kernel_main()
{
clear_screen();
char* str = "rewqtqrewqre";
for (int i = 0; i < 6; ++i)
{
print_char(str, i, 10, WHITE_ON_BLACK);
}
}
```
Any info for why this may not work would be greatly appreciated, if you would like to see more of the code my github repo is here: https://github.com/finlaymorrison/os
```c
#include "../drivers/ports.h"
#include "../drivers/screen.h"
void print_first_char(char* str)
{
print_char(str[0], 0, 10, WHITE_ON_BLACK);
}
void kernel_main()
{
clear_screen();
char* str = "rewqtqrewqre";
print_char(str[0], 0, 10, WHITE_ON_BLACK);
print_first_char(str);
}
```
Will sucsessfuly print the 'r' character when the `print_first_char` function is commented out, however upon uncommenting it it no longer works,
I also found that looping through a string with a for loop does not work, in the following code, the top block works fine, and the bottom one does not.
```c
void kernel_main()
{
clear_screen();
char* str = "rewqtqrewqre";
print_char(str[0], 0, 10, WHITE_ON_BLACK);
print_char(str[1], 1, 10, WHITE_ON_BLACK);
print_char(str[2], 2, 10, WHITE_ON_BLACK);
print_char(str[3], 3, 10, WHITE_ON_BLACK);
print_char(str[4], 4, 10, WHITE_ON_BLACK);
print_char(str[5], 5, 10, WHITE_ON_BLACK);
}
```
and this code does not work:
```c
void kernel_main()
{
clear_screen();
char* str = "rewqtqrewqre";
for (int i = 0; i < 6; ++i)
{
print_char(str, i, 10, WHITE_ON_BLACK);
}
}
```
Any info for why this may not work would be greatly appreciated, if you would like to see more of the code my github repo is here: https://github.com/finlaymorrison/os