Results 1 to 8 of 8

Thread: Dear strings....

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Dear strings....

    Trying to copy one string to another using strcpy but always crashes:
    PHP Code:
    char *tempcaption;
    strcpy(tempcaptioncLabel::caption); 
    "cLabel::caption" is declared as "char *caption".
    Baaaaaaaaah

  2. #2
    amac
    Guest
    You must pass in something that is previously allocated.

    Either

    Code:
    char szBuffer[32] = "";
    strcpy( szBuffer, "Alright" );
    or

    Code:
    char * pszBuffer = new char[32];
    strcpy( szBuffer, "Alright" );
    delete [] pszBuffer;

    I suppose you could always do this too


    Code:
    char * pszBuffer = strdup( "Word" );
    free( pszBuffer );

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I am trying to keep away from char array (the one declared as an array) that's why I have to mess up with the pointers. I just got it working by using:
    PHP Code:
    char *tempcaption cLabel::caption;
    cLabel::caption szCaption
    I thought you couldn't use = sign to set the value of one string to another.
    Baaaaaaaaah

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't. You only assign pointer values here. When you modify one string now, all others will be modified too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    So is this right:
    PHP Code:
    char *cLabel::SetCaption(char szCaption[])
    {
        
    char *tempcaption cLabel::caption;
        
    cLabel::caption szCaption;
        return (
    char*)tempcaption;
    }

    char *cLabel::GetCaption()
    {
        return (
    char*)cLabel::caption;

    None of the functions return an address to a local variable or a variable from the class?
    Baaaaaaaaah

  6. #6
    amac
    Guest
    you have to use something strcpy() like CornedBee said you are only copying the pointer's not the actual character array.


    See above examples.

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Try dereferencing the pointers maybe?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, if he only returns the class members the get-version is ok.
    The SetCaption can't work this way, you don't return the old caption, but rather the new.
    Really, since this is MFC (I think, seems like it), why don't you just store the caption in a CString and return string from the Set function. It makes life so much easier. And if it's not MFC, use string. It's better than CString most of the time anyway.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width