How to pass a char* string parameter to a function

I have a function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char*
wchars_to_chars ( wchar_t* wchar_string )
///////////////////////////////////////
// convert wchar string to char string
// see also:
// http://msdn.microsoft.com/en-us/library/ms235631(v=VS.90).aspx
// Article title : "How to: Convert Between Various String Types"
{
    size_t wchar_string_size = wcslen( wchar_string ) + 1;
    size_t number_of_characters_converted = 0;
    const size_t char_string_size = wchar_string_size * 2;
    char* char_string = new char[ char_string_size ];
    wcstombs_s ( &number_of_characters_converted,
				  char_string,
				  char_string_size,
				  wchar_string,
				 _TRUNCATE );
	// see detail of this function at: 
	// http://msdn.microsoft.com/en-us/library/s7wzt4be(v=VS.90).aspx
	return char_string;
}


By this function, can I pass a parameter and get the result like this? :
1
2
3
4
char* cs = "";
wchar_t* ws = "data";

cs = wchars_to_chars ( ws );


I know it can be used like this:
1
2
3
4
5
6
7
8
9
10
11
char* cs = new char [ 10 ];
wchar_t* ws = new wchar_t [ 10 ];
strcpy ( cs, ""     );
wcscpy ( ws, "data" );

cs = wchars_to_chars ( ws );

delete [] cs;
cs = NULL;
delete [] ws;
ws = NULL;
ur wchar_t* ws = "data" is incorrect. all widechar assignment needs the letter 'L' in front of it.

this is how u can fix the code.

1
2
3
4
char* cs = "";
wchar_t* ws = L"data";

cs = wchars_to_chars ( ws );
Topic archived. No new replies allowed.