|
-
Jun 4th, 2002, 09:12 PM
#1
Thread Starter
PowerPoster
Dear strings....
Trying to copy one string to another using strcpy but always crashes:
PHP Code:
char *tempcaption;
strcpy(tempcaption, cLabel::caption);
"cLabel::caption" is declared as "char *caption".
-
Jun 4th, 2002, 09:15 PM
#2
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 );
-
Jun 4th, 2002, 09:25 PM
#3
Thread Starter
PowerPoster
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.
-
Jun 5th, 2002, 04:38 AM
#4
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.
-
Jun 5th, 2002, 10:06 AM
#5
Thread Starter
PowerPoster
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?
-
Jun 5th, 2002, 04:28 PM
#6
you have to use something strcpy() like CornedBee said you are only copying the pointer's not the actual character array.
See above examples.
-
Jun 5th, 2002, 06:22 PM
#7
Frenzied Member
Try dereferencing the pointers maybe?
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jun 6th, 2002, 08:01 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|