Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strings

Posted on 2000-02-24
6
Medium Priority
?
162 Views
Last Modified: 2010-04-02
Hello experts...

First I'm programing for windows.

Well...
Some api calls returns a pointer to a NULL terminated character string.
The problem is... I'd like to copy that string to another place in memory...
In VB I'd do this:

A$ = B$.... But in C++ the things doesn't seem to be that easy...

I guess there is some runtime library or may be... I should to write code for that...
Well... What do I do?...
If someone has some code sample to do that... I will apretiate...

Thanks...


0
Comment
Question by:Marsc
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 180 total points
ID: 2556858
char *pszStr1 = "abc";

char achStr2[32];

strcpy(achStr2, pszStr1);

You may also use the Windows API function lstrcpy.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2557247
You could also use the standard string class to simplify things.

i.e.

#include <string>
using namespace std;

....
string str = FunctionReturningChars();

then str contains a copy of the string, and you can do assignment with C++ strings as you can with your VB strings, e.g.:

string a, b;
a = b;
0
 
LVL 1

Expert Comment

by:Ernest022699
ID: 2559085
chensu is correct if the maximum possible size of the string is known at compile time.  A cleaner way to do the task is to malloc exactly enough memory and then do the copy (remembering to call free later, of course).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:chensu
ID: 2559379
Right.

char *pszStr1 = "abc";

char *pszStr2 = new char[strlen(pszStr1) + sizeof(char)];

strcpy(pszStr2, pszStr1);

....

delete []pszStr2;
0
 
LVL 1

Author Comment

by:Marsc
ID: 2563137
Adjusted points to 60
0
 
LVL 1

Author Comment

by:Marsc
ID: 2563139
Thank you very much again...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question