What is the proper way to create a temporary char* and print to it?

listed in answer

What is the proper way to create a temporary char* and print to it?
0 votes, 0.00 avg. rating (0% score)

ANSWER:

The problem with your first one is not in the printing, but in the returning. You’re returning a pointer to an array which has been reclaimed (because it is an automatic variable, its lifetime ends when the function returns).

Instead try:

string byteArrayToString(const unsigned char* const byte)

    char t[18] = "";
    sprintf(t, "%02X:%02X:%02X:%02X:%02X:%02X", byte[0], byte[1], byte[2], byte[3], byte[4], byte[5]);
    return t;
}

by Ben Voigt from http://stackoverflow.com/questions/10264603