pointer-to-two-adjactend-chars

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • .rhavin grobert

    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::GetBuf fer(int);
    int CString::GetLen ght(int);
    _______________ _______________ ____________

    // code:

    CString str ="SomeStringWit hMoreThanOneTCh ar";
    TCHAR *(pEnd[2]) = /* § */ str.GetBuffer(s tr.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?

  • Victor Bazarov

    #2
    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::GetBuf fer(int);
    int CString::GetLen ght(int);
    _______________ _______________ ____________
    >
    // code:
    >
    CString str ="SomeStringWit hMoreThanOneTCh ar";
    TCHAR *(pEnd[2]) = /* § */ str.GetBuffer(s tr.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_cas t<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(st r.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

    Comment

    • peter koch

      #3
      Re: pointer-to-two-adjactend-chars

      On 24 Sep., 18:53, ".rhavin grobert" <cl...@yahoo.de wrote:
      this is m$ vc++, but the question itself is c++ specific...
      >
      TCHAR is a 'char' in non-unicode.
      >
      functions return...
      TCHAR* CString::GetBuf fer(int);
      int CString::GetLen ght(int);
      _______________ _______________ ____________
      >
      // code:
      >
      CString str ="SomeStringWit hMoreThanOneTCh ar";
      TCHAR *(pEnd[2]) = /* § */ str.GetBuffer(s tr.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

      Comment

      • gw7rib@aol.com

        #4
        Re: pointer-to-two-adjactend-chars

        On 24 Sep, 17:53, ".rhavin grobert" <cl...@yahoo.de wrote:
        this is m$ vc++, but the question itself is c++ specific...
        >
        TCHAR is a 'char' in non-unicode.
        >
        functions return...
        TCHAR* CString::GetBuf fer(int);
        int CString::GetLen ght(int);
        _______________ _______________ ____________
        >
        // code:
        >
        CString str ="SomeStringWit hMoreThanOneTCh ar";
        TCHAR *(pEnd[2]) = /* § */ str.GetBuffer(s tr.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.

        Comment

        Working...