Thread: copy LPCSTR type

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    copy LPCSTR type

    Hi

    I am having a problem to copy a LPCSTR string to the same type variable...

    I have allready tried
    Code:
    LPCSTR lpStringOne;
    LPCSTR lpStringTwo;
    
    lpStringOne = lpStringTwo;
    wsprintf(lpStringOne, "%s", lpStringTwo);
    strcpy(lpStringOne, lpStringTwo);
    Either function failed to work... When the programme came to the function it failed.

    Which function shall I use in case of LPCSTR?

    Thx for responses

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well they're just pointers (you really need to understand "Hungarian Notation" as used by Microsoft to understand that).

    Somewhere along the line, you need to make those pointer point to a real string of some sort, or some actual space to store a string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I am having a problem to copy a LPCSTR string to the same type variable...
    That's probably because an LPCSTR is nothing more than a "const char*". You cannot write to it for two reasons: You will need a real array of chars and you will need a non-const pointer to it to use strcpy or sprintf.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    try this -

    Code:
    LPCSTR lpStringOne;
    LPCSTR lpStringTwo;
     
    lpStringTwo = (LPCSTR)malloc(512);
    
    lpStringOne = lpStringTwo;
    wsprintf(lpStringOne, "%s", lpStringTwo);
    strcpy(lpStringOne, lpStringTwo);
     
    free(lpStringTwo);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Changing an objects Type
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2004, 07:18 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM