How to serialize to char* using Google Protocol Buffers?

You can serailze the output to a ostringstream and use stream.str() to get the string and then access the c-string with string.c_str().

std::ostringstream stream;
address_book.SerializeToOstream(&stream);

string text = stream.str();
char* ctext = text.c_str();

Don't forget to include sstream for std::ostringstream.


That's easy:

size_t size = address_book.ByteSizeLong(); 
void *buffer = malloc(size);
address_book.SerializeToArray(buffer, size);

Check documentation of MessageLite class also, it's parent class of Message and it contains useful methods.