#includeAString str("hello"); const char* cstr = str.c_str(); printf("%s\n", cstr); // prints "hello"
#includeIn this example, an AString object is created with the value "world". The c_str() function is then called on this object to obtain a pointer to the underlying character array. This pointer is assigned to a const char* variable and printed out. The string is then modified by appending a "!" character. However, since the c_str() function returns a const pointer that points to the original character array, the printed string is still "world". The AString class is part of the AUnit library, which is a lightweight unit testing framework for C and C++.AString str("world"); const char* cstr = str.c_str(); str += "!"; printf("%s\n", cstr); // prints "world"