2010年12月2日 星期四

how to write a function return char*


A way to do it is if you really want to return the char *, you could do this:


char* newpwd(char *ptr)
{
char *buf = new char[33];

MD5Data(ptr,strlen(ptr),buf);
printf("%s",buf);
return buf;
}


This will allocate the string on the heap, which lives after the function returns. You'll just have to make sure you do a "delete [] buf;" when you're done with the data, or you will have a memory leak. I'm not sure which way is preferable, but I think this should work.

0 意見: