G
Gary Wessle
string a = "abcdef";
aa[80] = a.c_str();
bb[40] = how can I get def from aa in here?
thanks
aa[80] = a.c_str();
bb[40] = how can I get def from aa in here?
thanks
string a = "abcdef";
aa[80] = a.c_str();
bb[40] = how can I get def from aa in here?
thanks
string a = "abcdef";
aa[80] = a.c_str();
bb[40] = how can I get def from aa in here?
Gary said:string a = "abcdef";
aa[80] = a.c_str();
bb[40] = how can I get def from aa in here?
SirMike said: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
Gary Wessle said:SirMike said: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());
SirMike said: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());
BobR said:void func1( char const *p, std:stream &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<<std::endl;
return;
}
{
std::string a = "abcdef";
func1( a.c_str(), std::cout );
}
// out: frst=abc last=def
Gary said:string a = "abcdef";
aa[80] = a.c_str();
bb[40] = how can I get def from aa in here?
Default User wrote in message said:BobR wrote:
Did you actually try this?
Yes.
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.
Default User wrote in message said:BobR wrote:
Outputting the result was undefined behavior.
#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()const {return _frst;}
const char* myType::second()const {return _second;}
int main(){
string s = "abc-def";
Q%G.ANoN?N=NoN?N=NoN?N=NoN?N=%@`%G.ANoN?N=%@M$B'\(B$myType mt(s.c_str());
string tmp = mt.frst();
string tmp2 = mt.second();
cout << tmp << " " << tmp2 << endl;
}
//output
abcMh
in order to get the clean output "abd def" I did some changes to the
code but now getting Seg Fault.
#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); _frst[ 4 ] = 0;
strncpy(_second, pair+4, 3); _second[ 4 ] = 0;
}
const char* myType::frst()const {
char* r;
const char* myType::second()const {
char* r;
strncpy(r,_second,3);
return r;
}
int main(){
string s = "abc-def";
myType mt(s.c_str());
cout << mt.frst() << " " << mt.second() << endl;
}
BobR said:Default User wrote in message said:BobR wrote:
Outputting the result was undefined behavior.
[ ref: the missing terminator. ]
HEY! How come why for you yell at me but not Jim?
<G>
Default User wrote in message ...Outputting the result was undefined behavior.
[ ref: the missing terminator. ]
HEY! How come why for you yell at me but not Jim?
<G>
Because I didn't try to output the result.
Yeah said:I suggested he use std::string anyway.
Old said:Gary said:string a = "abcdef";
aa[80] = a.c_str();
This is an error: you can't assign to arrays
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.