By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,377 IT Pros & Developers. It's quick & easy.

copy char*

P: n/a

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

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

thanks
Nov 15 '06 #1
Share this Question
Share on Google+
20 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
SirMike <si*****@FUCKSPAMMERSpoczta.onet.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

P: n/a
I want frst to have "abc" and last to have "def".
Nov 15 '06 #6

P: n/a
"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain...
SirMike <si*****@FUCKSPAMMERSpoczta.onet.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

P: n/a

Gary Wessle wrote in message ...
>SirMike <si*****@FUCKSPAMMERSpoczta.onet.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<<std::endl;
return;
}

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

--
Bob R
POVrookie
Nov 15 '06 #8

P: n/a
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<<std::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

P: n/a

#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";
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

P: n/a
Gary Wessle wrote:
string a = "abcdef";
aa[80] = a.c_str();
This is an error: you can't assign to arrays
bb[40] = how can I get def from aa in here?
Assuming you have managed to get "a" into "aa" successfully:
memcpy(bb, aa + 3, 3);

Nov 16 '06 #11

P: n/a
I need it to ouput
abc def

what must be done to
strncpy(_frst, pair, 3);
strncpy(_second, pair+4, 3);
in the c'tor?
Nov 16 '06 #12

P: n/a

Default User wrote in message <4s************@mid.individual.net>...
>BobR wrote:
<snip **incomplete** func1>
>// out: frst=abc last=def

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.
Of course you are correct. My bad. (gads, I hate that phrase!!).
Thanks for the 'heads-up'.

<?>
It must have been that the char arrays were initialized to zeros by my
compiler.
<checking....>
Nope!!! Just happened to be a zero at 4th position in both arrays.
[....even on re-compiles. ....and adding another array before those.]
[ I can't seem to break it! ]
</?>

Murphy's "bad luck" clause strikes again!!

Thanks again for the correction.
--
Bob R
POVrookie
Nov 16 '06 #13

P: n/a

Default User wrote in message <4s************@mid.individual.net>...
>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>

--
Bob R
POVrookie
Nov 16 '06 #14

P: n/a
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);
strncpy(_second, pair+4, 3);
}
const char* myType::frst()const {
char* r;
strncpy(r,_frst,3);
return 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;
}
Nov 16 '06 #15

P: n/a

Gary Wessle wrote in message ...
>
#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);
// Like Default User pointed out, I forgot to terminate the array:
_frst[4] = '\0';
strncpy(_second, pair+4, 3);
_second[4] = '\0';

// or:
// frst[4]= 0;
// second[4]= 0;

// also, BAD nameing.
// Put the underscore on the end (not the begin) of the names
// if you MUST use them.
>}
const char* myType::frst()const {return _frst;}
const char* myType::second()const {return _second;}

int main(){
string s = "abc-def";
// You changed the spec on us!
// I'll let you figure out the 'fix' as punishment.
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

You were luckier than I was!! <G>

--
Bob R
POVrookie

Nov 16 '06 #16

P: n/a

Gary Wessle wrote in message ...
>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;
OK, when working with raw arrays, you always need to ask yourself, "Is there
enough room in there?".
You declared 'r' as a pointer to type 'char', but, where is the storage for
the 'strncpy' to copy to? You need to make some storage, and then put the
address of that into 'r'.
You are getting the 'Seg Fault' because you did not point 'r' to anything, or
initialize it. It's pointing to something that is not yours to change.

Now before you get the idea to do:

char *r( new char[ 40 ] );

....remember that now you are putting the life of that pointed to storage in
the callers hands, it's up to them to see that it is eventually
'delete[]'ed!!
Hint: 'cout' don't know how to fo that!
The easy fix is to dump the 'strncpy' and 'char*' here, and just do:

return _frst;
// strncpy(r,_frst,3);
// return r;
}
>const char* myType::second()const {
char* r;
Same here.
strncpy(r,_second,3);
return r;
}

int main(){
string s = "abc-def";
myType mt(s.c_str());
cout << mt.frst() << " " << mt.second() << endl;
}
Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

--
Bob R
POVrookie
--
Get "Thinking in C++", 2nd ed. Volume 1(&2) by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
Nov 16 '06 #17

P: n/a
"BobR" <Re***********@worldnet.att.netwrote in message
news:m0********************@bgtnsc04-news.ops.worldnet.att.net...
>
Default User wrote in message <4s************@mid.individual.net>...
>>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>
Because I didn't try to output the result. I just gave the op what he asked
for, abc in the first, cdf in the second. He didn't ask to terminate them,
nor even why he was doing it. Now, if he actually wants to output them, I
would do it a different way. I suggested he use std::string anyway.
Nov 16 '06 #18

P: n/a

Jim Langston wrote in message ...
>"BobR" wrote in message...
>>
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, <pointing at headI gots no kidneys!!
I suggested he use std::string anyway.
Must be some 'learning' thing, std::string would be too easy.

--
Bob R
POVrookie
Nov 16 '06 #19

P: n/a
Old Wolf wrote:
Gary Wessle wrote:
string a = "abcdef";
aa[80] = a.c_str();

This is an error: you can't assign to arrays
But you can to an array element, which is why his missing declarations
for things like aa made the problem insoluable.

If aa were declared something like:

const char *aa[81];

Then you could assign the results from c_str() to that element. Now,
that's apparently not what he was going for.


Brian
Nov 16 '06 #20

P: n/a
BobR wrote:
>
Default User wrote in message <4s************@mid.individual.net>...
BobR wrote:
<snip incomplete func1>
// out: frst=abc last=def
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.

Of course you are correct. My bad. (gads, I hate that phrase!!).
Thanks for the 'heads-up'.
It happens. Generally, strncpy() is a tricky and not overly useful
function. I usually, when doing this in C, try to use strncat(). That
requires initializing the target string first.
char target[4] = "";

char source[] = "abcdef";

strncat(target, source, 3);
That will give you a null-terminated string in target consisting of the
first three characters of source.


Brian
Nov 16 '06 #21

This discussion thread is closed

Replies have been disabled for this discussion.