Solved

Strings

Posted on 2000-02-24
6
155 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now