473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pointer-to-two-adjactend-chars

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;
// 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.
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?

Sep 24 '08 #1
3 1602
..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
Sep 24 '08 #2
On 24 Sep., 18:53, ".rhavin grobert" <cl...@yahoo.dewrote:
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;
// in other words: a pointer to the second-last TCHAR is returned
It does not look like that to me. I do not know CString, but this
looks like a runaway buffer.
__________________________________________

in the place marked "/* § */", a cast is neccesary. My problem: i dont
find the correct syntax.
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?
You can't without a cast, but does it matter? If you used a char* (I
am surprised you don't use a char const*), char[0] points to the
second last character and char[1] to the last (assuming that your code
is correct, of course).

/Peter
Sep 24 '08 #3
On 24 Sep, 17:53, ".rhavin grobert" <cl...@yahoo.dewrote:
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;
// 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.
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?
Is there some reason why you need a pointer to two TCHARs? Can't you
make do with a pointer to the first one, and look at the next location
to find the second one when you need it?

I mean in a similar way to the the way strings are handled in C. You
don't actually have a pointer to the "whole string", you (normally)
have just a pointer to the first character of it. But from this
pointer you can find the second, third etc characters when you need
them.

Incidentally, your code looks odd, using chars in some places and
TCHARs in others. I'm not sure what use this is. If the code is being
written purely for non-Unicode, then there seems no reason to use
TCHARs at all. Whereas, if the code might one day be used with
Unicode, you would find it convenient to have it all set up that way -
which would require adding "_TEXT"s and the like to your code.

Hope that helps.
Paul.
Sep 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rasti | last post by:
Hello, I am trying to translate a C++ script to Java. Are there any differences between the C++ this-pointer and the Java this-pointer? Thank you
5
by: | last post by:
What is the difference between passing argument by reference or by pointer? Is there any difference in terms of PERFORMANCE ? When and why one of these method (by reference or by pointer) should be...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
20
by: Bill Potter | last post by:
I am a learning programmer in C and i want to know why some one would use pointers instead of going direct!
4
by: code break | last post by:
Hi all, What is the difference between stack pointer and frame pointer ? Any suggestions are welcome ,,,
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.