Re: pointer-to-two-adjactend-chars
..rhavin grobert wrote:
this is m$ vc++, but the question itself is c++ specific...
TCHAR is a 'char' in non-unicode.
functions return...
TCHAR* CString::GetBuffer(int);
int CString::GetLenght(int);
__________________________________________
// code:
CString str ="SomeStringWithMoreThanOneTChar";
TCHAR *(pEnd[2]) = /* ? */ str.GetBuffer(str.GetLength()) +
str.GetLength() - 2;
So, 'pEnd' is *an array of two pointers to TCHAR*. You can't initialise
an array with a pointer in C++. The only special case is initialisation
of an array of char with a literal, but it doesn't apply here.
// in other words: a pointer to the second-last TCHAR is returned
__________________________________________
in the place marked "/* ? */", a cast is neccesary. My problem: i dont
find the correct syntax.
There isn't any, most likely.
the error given is "cannot convert from 'char *' to 'char *[2]' ", but
inserting
(char *[2]), (char (*[2])), ((char*) [2]) or (*(char[2])) doesnt work.
Anyone knows the correct syntax for a cast to a pointer-to-two-
adjactend-TCHARs please?
There is no such type as *a pointer to two adjacent TCHAR*. There can
be a pointer to an array of two TCHAR, which is
TCHAR (*pEnd)[2]
or a simple pointer to TCHAR, which you already know
TCHAR *pEnd
Now, the assuredness that there is a second TCHAR that follows the one
to which that pointer would point, cannot really be expressed by means
of the language itself, AFAIK.
If you want to use a pointer to an array of two TCHAR, you can do
TCHAR (*pEnd)[2] = reinterpret_cast<TCHAR(*)[2]>(...
but one problem with it is that you need to dereference the pointer
before you can index within the array:
(*pEnd)[0]; // the first TCHAR in the array
(*pEnd)[1]; // the second TCHAR in the array
and another problem is that the language does not provide the mechanism
that would verify that there is the second TCHAR. Both of those should
lead you to the conclusion to use a regular pointer to TCHAR:
TCHAR *pEnd = tr.GetBuffer(str.GetLength()) + str.GetLength() - 2;
(also, check with CString interface, 'GetBuffer' may require releasing
the buffer later, and holding onto a pointer from an object's internal
data may not be such a good idea).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask