Skip to main content
gamedev.net
🔒 Locked

Question about pointers

Started by shvalb Feb 22, 2005 at 5:01 AM 0 replies 350+ views
Original Post
shvalb
shvalb
Hi. I have a a little code that doesn't do the job, it's a function that gets as a a parameter **string allocates it in the function and return it with some data in it, the problem is that it doesn't return right, here is the code, if someone could help me understand what I'm doing wrong.. void CWideCharConvert::convertWideCharToString(WCHAR *wcharArr, char **string) { int wcharArrLength, i, k=0; char temp[10]; wcharArrLength = ::wcslen(wcharArr); string = new char*[sizeof(char)]; string[0] = new char[wcharArrLength * 6 + 1]; MyUniCode *pUni = (MyUniCode *)wcharArr; for(i=0; i < wcharArrLength; i++) { ::sprintf(temp, "%d%d", pUni->charValAndLang[k+1], pUni->charValAndLang[k]); if(k==0) { ::strcpy(*string, temp); } else { ::strcat(*string, temp); } k+=2; } } the 'string' is defined as : char **string = NULL; when the function end it is still NULL. Thanks
IFooBar
IFooBar
If you want a value to be changed. Then you have to use references. SO if you wanted to pass in a char** to a function and you wanted that variable to change it's value, then you need to pass in a char**&. But I see that you are trying to convert a wstr to a cstr? You can use this: (untested code)

void towcs( char*& strOut, const wchar_t* wstr ){  strOut = new char[wcslen(wstr) + 1];  wcstombs( strOut, wstr );}// then use like this:char* string = 0;towcs( string, L"Some Wide string" );// another option using a pointer to a pointer (which I// this is what you were trying to do?void towcs( char** strOut, const wchar_t* wstr ){  *strOut = new char[wcslen(wstr) + 1];  wcstombs( *strOut, wstr );}// then use like this:char* string = 0;towcs( &string, L"Some Wide string" );
[size=2]aliak.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.