473,785 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy char*


string a = "abcdef";
aa[80] = a.c_str();

bb[40] = how can I get def from aa in here?

thanks
Nov 15 '06 #1
20 5095

Gary Wessle napsal:
string a = "abcdef";
aa[80] = a.c_str();

bb[40] = how can I get def from aa in here?

thanks
a.c_str() + 4

But you should know, that result of c_str() method is valid only till
the first change in `a'. So you should make copy of i if you need
C-string and cannot guarantee, that no change in `a' will occure.

Nov 15 '06 #2
Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisa³(a):
string a = "abcdef";
aa[80] = a.c_str();

bb[40] = how can I get def from aa in here?
use strncpy

--
SirMike - http://www.sirmike.org

C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg. - Bjarne Stroustrup
Nov 15 '06 #3
Gary Wessle wrote:
>
string a = "abcdef";
aa[80] = a.c_str();

bb[40] = how can I get def from aa in here?

What are aa and bb?

Brian
Nov 15 '06 #4
SirMike <si*****@FUCKSP AMMERSpoczta.on et.plwrites:
Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisał(a):
string a = "abcdef";
aa[80] = a.c_str();

bb[40] = how can I get def from aa in here?

use strncpy
void func1(const char* p){
char frst[40];
char last[40];
strncpy(frst, p, 3); //first 3 ????
strncpy(last, p, 3); //last 3 ?????
}

string a = "abcdef";
func1(a.c_str() );
Nov 15 '06 #5
I want frst to have "abc" and last to have "def".
Nov 15 '06 #6
"Gary Wessle" <ph****@yahoo.c omwrote in message
news:m3******** ****@localhost. localdomain...
SirMike <si*****@FUCKSP AMMERSpoczta.on et.plwrites:
>Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisal(a):
string a = "abcdef";
aa[80] = a.c_str();

bb[40] = how can I get def from aa in here?

use strncpy

void func1(const char* p){
char frst[40];
char last[40];
strncpy(frst, p, 3); //first 3 ????
strncpy(last, p, 3); //last 3 ?????
}

string a = "abcdef";
func1(a.c_str() );
void func1( const char* p )
{
char first[40];
char last[40];
strncpy( first, p, 3 );
strncpy( last, p + 3, 3 );
}

Ugly code. Prefer std::string instead.

void func1( const std::string Value )
{
std::string first = Value.substr( 0, 3 );
std::string last = Value.substr( 4, 3 );
}
Nov 15 '06 #7

Gary Wessle wrote in message ...
>SirMike <si*****@FUCKSP AMMERSpoczta.on et.plwrites:
>use strncpy

void func1(const char* p){
char frst[40];
char last[40];
strncpy(frst, p, 3); //first 3 ????
strncpy(last, p, 3); //last 3 ?????
}

string a = "abcdef";
func1(a.c_str( ));
void func1( char const *p, std::ostream &out ){
char frst[ 40 ];
char last[ 40 ];
strncpy( frst, p, 3 ); //first 3 ????
strncpy( last, p+3, 3 ); //last 3 ?????
out << "frst="<<frst<< " last="<<last<<s td::endl;
return;
}

{
std::string a = "abcdef";
func1( a.c_str(), std::cout );
}
// out: frst=abc last=def

--
Bob R
POVrookie
Nov 15 '06 #8
BobR wrote:
void func1( char const *p, std::ostream &out ){
char frst[ 40 ];
char last[ 40 ];
strncpy( frst, p, 3 ); //first 3 ????
strncpy( last, p+3, 3 ); //last 3 ?????
out << "frst="<<frst<< " last="<<last<<s td::endl;
return;
}

{
std::string a = "abcdef";
func1( a.c_str(), std::cout );
}
// out: frst=abc last=def

Did you actually try this? If you got the purported results, then it
was by sheer bad luck. You didn't terminate the string, and strncpy
doesn't do it for you. Outputting the result was undefined behavior.


Brian
Nov 15 '06 #9

#include <string>
#include <iostream>
using namespace std;

class myType {
protected:
char _frst[40];
char _second[40];
char _pair[80];
public:
const char* frst() const;
const char* second() const;
myType(const char* pair);
};

myType::myType( const char* pair)
{
strncpy(_frst, pair, 3);
strncpy(_second , pair+4, 3);
}
const char* myType::frst()c onst {return _frst;}
const char* myType::second( )const {return _second;}

int main(){
string s = "abc-def";
myType mt(s.c_str());
string tmp = mt.frst();
string tmp2 = mt.second();
cout << tmp << " " << tmp2 << endl;
}

//output
abcMh Q%G.ANoN?N =NoN?N=NoN ?N=NoN?N=% @`%G.ANoN? N=%@M$B'\(B $B%G.ANoN? N=(B$B%@(B def
Nov 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7172
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
6
9492
by: Charles Herman | last post by:
I am using push_back to place a class object into a vector. This class contains data members that are pointers to a char string. When I use push_back, I believe a copy of the object is first made, and then deposited into the vector. But I want this copy to not mereley copy the pointer to the char string, but rather, I want to malloc new space, and make a copy of the char string. The default copy constructor is defined as
22
2864
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member function func(const String &param). When an actual String is passed as the param this is nice and efficient. I have a constructor which takes a const char* as an argument. And
1
2225
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
4
8826
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
11
1821
by: csudha | last post by:
Hi all, Can you please explain me the usage of copy constructor and how do we use it. Give me some example. Regards, Sudha.
6
1543
by: Gary | last post by:
I'm confused about the two ways of string copy below. The first one (two functions) works; the second doesn't. Can anybody explain the reason to me? Thanks a lot. #include <stdio.h> /*this one works well void copy (char *s, char *t) { int i = 0;
5
2444
by: sarathy | last post by:
Hi, I need to see the output of the program when no copy constructor is used. Since by default, there is an implicit one, how should i perform the override, so as to totally remove the copy constructor functionality. Consider the following code. class Employee {
11
3438
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
20
432
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object point to the same address . In this case any Of the two Object is destroyed a memory leak occurs as one of the object stays on the Heap . My experiments on the same have proved otherwise .
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.