class cString {
public:
cString(const char *cStr) : m_cstr(cStr) {}
void printAddr_m_cstr() {
// commenta la riga seguente per vedere come
// cambia l'indirizzo che da 0x212 a 0x1EE
Serial.print("Indirizzo di membro privato m_cstr: ");
Serial.println((uint16_t)&m_cstr, HEX);
}
void printDataOf_m_cstr() {
Serial.print("indirizzo a cui punta m_cstr: ");
Serial.println((uint16_t)m_cstr, HEX);
}
private:
const char *m_cstr = nullptr;
};
/*### Nota che gli indirizzi variano se
aggiungiamo codice
###*/
#define ADDR_OF_STR_LITERAL 0x115
#define ADDR_OF_M_CSTR 0x22E
#define ADDR_OF_PTRPTR_TO_STR 0x8FA
cString myCstr("Questa è c string");
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myCstr.printAddr_m_cstr();
myCstr.printDataOf_m_cstr();
// ADDR_OF_STR_LITERAL 0x115
const char *ptrToStr = (char*)ADDR_OF_STR_LITERAL;
Serial.println(ptrToStr);
/* Dall'indirizzo 0x212 ricavo nuovamente
la stringa "Questa è c string", per fare
ciò mi serve una variabile puntatore
a puntatore, come segue:
*/
// ADDR_OF_M_CSTR 0x22E
const char **ptrptrToStr = (char**)ADDR_OF_M_CSTR;
Serial.println(*ptrptrToStr);
/* Indirizzo valore
0x212 0x112
0x112 Q
Ma allora anche ptrptrToStr ha indirizzo è valore.
Il valore lo conosciamo ed è 0x212 mentre per
l'indirizzo dobbiamo ricavarlo così:
*/
Serial.print("indirizzo di ptrptrToStr: ");
Serial.println((uint16_t)&ptrptrToStr, HEX);
/* Se volessi stampare nuovamente "Questa è c string"
partendo dall'indirizzo 0x8FA dovrò creare
un nuovo puntatore a puntatore a puntatore, cioè
***anothePtr e poi stampare con:
Serial.println(**anothePtr);
*/
// ADDR_OF_PTRPTR_TO_STR 0x8FA
const char ***anotherPtr = (char***)ADDR_OF_PTRPTR_TO_STR;
Serial.println(**anotherPtr);
}
void loop() {
// put your main code here, to run repeatedly:
}