Solved

Strings

Posted on 2000-02-24
6
157 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 60 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

786 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