Trying to copy one string to another using strcpy but always crashes:
"cLabel::caption" is declared as "char *caption".PHP Code:char *tempcaption;
strcpy(tempcaption, cLabel::caption);
Printable View
Trying to copy one string to another using strcpy but always crashes:
"cLabel::caption" is declared as "char *caption".PHP Code:char *tempcaption;
strcpy(tempcaption, cLabel::caption);
You must pass in something that is previously allocated.
Either
orCode:
char szBuffer[32] = "";
strcpy( szBuffer, "Alright" );
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 );
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:
I thought you couldn't use = sign to set the value of one string to another.PHP Code:char *tempcaption = cLabel::caption;
cLabel::caption = szCaption;
You can't. You only assign pointer values here. When you modify one string now, all others will be modified too.
So is this right:
None of the functions return an address to a local variable or a variable from the class?PHP Code:char *cLabel::SetCaption(char szCaption[])
{
char *tempcaption = cLabel::caption;
cLabel::caption = szCaption;
return (char*)tempcaption;
}
char *cLabel::GetCaption()
{
return (char*)cLabel::caption;
}
you have to use something strcpy() like CornedBee said you are only copying the pointer's not the actual character array.
See above examples.
Try dereferencing the pointers maybe?
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.