views:

753

answers:

2

Please, how can I find out the length of a variable of type wchar_t* in c++?

code example below:

wchar_t* dimObjPrefix = L"retro_";

I would like to find out how many characters dimObjPrefix contains

+3  A: 
sizeof (wchar_t);

Edit:

I just noticed the string tag. If you want to know the size of a wchar_t string (wchar_t *), you might want to use

size_t wcslen (const wchar_t *ws);
Bertrand Marron
Perhaps sizeof (wchar_t), or wchar_t x followed by sizeof x?
Joseph Quinsey
@Joseph, it took me a long time to realize what you meant, I edited. Thank you. Sorry for the confusion.
Bertrand Marron
Regarding sizeof(), neither K does any SO reader happen to know?
Joseph Quinsey
@Joseph: What idiosyncrasy?
Dennis Zickefoose
`sizeof(type)` versus `sizeof expression`. Now if `e` is an expression, then `(e)` is also an expression, so `sizeof (expression)` is implicitly also allowed.
MSalters
A: 

Assuming that you want to get the length of null terminated C style string, you have two options:

  1. #include <cwchar> and use std::wcslen (dimObjPrefix);,
  2. or #include <string> and use std::char_traits<wchar_t>::length (dimObjPrefix);.
wilx