I am trying to do some manual paging for a small EEPROM with 8-Byte Page Write Buffer. The code I created works as I wanted in an online tool. But, when I implemented it within my programming environment, it gives me hard time particularly getting proper values based in parsed pages.
You may check yourself if you like; Click here for the code
or you can see from here as well;
#include <stdint.h>
#include <string.h>
#include <stdio.h>
int main(){
uint8_t * buffer = "123456789ABCDEFGHIJKLM";
uint8_t length = strlen(buffer);
uint8_t pagesize = 8;
uint8_t pagecount = length / pagesize;
if ((length % pagesize) >= 0){
pagecount++;
}
char memRet[8];
char *retVal = memRet;
for (uint8_t p = 1; p < pagecount + 1; p++){
printf("Page # %d => ", p);
retVal = GetPageParsed(buffer, pagesize, p);
printf("GetPageParsed : %s \n", retVal);
}
return 0;
}
uint8_t * GetPageParsed(uint8_t * buffer, uint8_t pageSize, uint8_t page){
char MemVal[8]; // If pageSize is used to determine the size of the array dynamically, for some reason, the array filled up with extra values!
char *RetVal = MemVal;
uint8_t i = 0;
uint8_t StartAddress = pageSize * (page - 1);
for (int p = StartAddress; p < pageSize * page; p++){
RetVal[i] = buffer[p];
i++;
}
return RetVal;
}
When I run it online, I have this outcome;
Page # 1 => GetPageParsed : 12345678
Page # 2 => GetPageParsed : 9ABCDEFG
Page # 3 => GetPageParsed : HIJKLM
But, when I run inside my program;
Page # 1 => 12345678\001\b\240/
Page # 2 => 9ABCDEFG\002\b\240/
Page # 3 => HIJKLM
And it is exactly the same code. But, in my program, it turns out different. Even though I dictate I need 8 bytes by allocating memory by char arrays (char memRet[8];
and char MemVal[8];
)
I am pretty sure I miss something. I appreciate your valuable inputs from now.