Solved

Strings

Posted on 2000-02-24
6
158 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

820 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