Thread: Typcasting string & LPSTR :: MFC

  1. #1
    kuphryn
    Guest

    Typcasting string & LPSTR :: MFC

    Hi.

    I would like to know a way to correctly cast a string object to LPSTR.

    I have an LVITEM structure for a CListCtrl.

    -----
    string szText = "testing 1 2 3";
    LVITEM item;
    ...

    // Compiler does not allow this kind of typecasting.
    // Note "testing 1 2 3" along is not an option. I can use a string
    // and/or CString object.

    item.pszText = szText;
    -----

    How do you get the line above to work? I have tried using a CString object, but the compiler kept responding with an error about LPSTR.

    Thanks,
    Kuphryn

  2. #2
    kuphryn
    Guest
    Do I need to allocate new memory and then copy the characters from string.c_str()?

    Kuphryn

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I would think that string.c_str() should work ok.

    If not then just declare it as the type required:

    LPSTR lpszStr="testing 1 2 3";

    LPSTR is a just pointer to a null terminated char array ie an ANSI string.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    CString szText = "testing 1 2 3";
    LVITEM item;
    item.pszText = szText.GetBuffer(0);
    You can get away with nthe above...

    Personally I would try to stick with the MFC CString as opposed to the STL version (if your in MFC....use MFC CString as its fits in with the rest of the framework)

    Also, unless you are going to alter and update this structure, use a constant string as opposed to a variable...it makes more sense

  5. #5
    kuphryn
    Guest
    Okay. Thanks.

    Fordy: Do you mean to add "const" declaration to any local variable that remains constant throughout the function call?

    Kuphryn

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by kuphryn
    Okay. Thanks.

    Fordy: Do you mean to add "const" declaration to any local variable that remains constant throughout the function call?

    Kuphryn
    Waht I mean is that if the string isnt likely to change huge amount, then use a char array

  7. #7
    kuphryn
    Guest
    Sometimes I call functions that may return a string object.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM