Solved

Strings

Posted on 2000-02-24
6
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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

Independent Software Vendors: 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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
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 how to clear a vector as well as how to detect empty vectors in C++.

751 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