I'm working on a project and I need to access an array of unsigned char that represent fonts for LCD. Some fonts need two to three bytes to represent each row. So I was trying to implement code like this
const unsigned char font[]={...};//array of n bytes representing a font.
uint8_t *ptr = (uint8_t*)font;
void lcd_map(){
//get 4 bytes of array as int to map LCD
int val = *(int*)ptr;
}
Dereferencing the pointer as int isn't working, debugging the code leads to an exception of type illegal access of address.
After some experimentation, I got it working when the assignment of the pointer and the dereferencing are done in the same scope:
const unsigned char font[]={...};//array of n bytes representing a font.
uint8_t *ptr;
void lcd_map(){
//get 4 bytes of array as int to map LCD
ptr = (uint8_t*)font;
int val = *(int*)ptr;
}
But I need to implement the function as the former code. What may be the issue? I tested the former code on an online C compiler and it works without a problem.