Connecting Tech Pros Worldwide Help | Site Map

copy char*

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2006, 10:25 AM
Gary Wessle
Guest
 
Posts: n/a
Default copy char*


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

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

thanks

  #2  
Old November 15th, 2006, 10:55 AM
ondra.holub
Guest
 
Posts: n/a
Default Re: copy char*


Gary Wessle napsal:
Quote:
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.

  #3  
Old November 15th, 2006, 11:05 AM
SirMike
Guest
 
Posts: n/a
Default Re: copy char*

Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisał(a):
Quote:
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
  #4  
Old November 15th, 2006, 03:55 PM
Default User
Guest
 
Posts: n/a
Default Re: copy char*

Gary Wessle wrote:
Quote:
>
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
  #5  
Old November 15th, 2006, 05:25 PM
Gary Wessle
Guest
 
Posts: n/a
Default Re: copy char*

SirMike <sirmike@FUCKSPAMMERSpoczta.onet.plwrites:
Quote:
Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisaƂ(a):
>
Quote:
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());
  #6  
Old November 15th, 2006, 05:45 PM
Gary Wessle
Guest
 
Posts: n/a
Default Re: copy char*

I want frst to have "abc" and last to have "def".
  #7  
Old November 15th, 2006, 06:35 PM
Jim Langston
Guest
 
Posts: n/a
Default Re: copy char*

"Gary Wessle" <phddas@yahoo.comwrote in message
news:m3odr84k8f.fsf@localhost.localdomain...
Quote:
SirMike <sirmike@FUCKSPAMMERSpoczta.onet.plwrites:
>
Quote:
>Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisal(a):
>>
Quote:
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 );
}


  #8  
Old November 15th, 2006, 07:05 PM
BobR
Guest
 
Posts: n/a
Default Re: copy char*


Gary Wessle wrote in message ...
Quote:
>SirMike <sirmike@FUCKSPAMMERSpoczta.onet.plwrites:
>
Quote:
>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


  #9  
Old November 15th, 2006, 08:45 PM
Default User
Guest
 
Posts: n/a
Default Re: copy char*

BobR wrote:
Quote:
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
  #10  
Old November 15th, 2006, 10:45 PM
Gary Wessle
Guest
 
Posts: n/a
Default Re: copy char*


#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
  #11  
Old November 15th, 2006, 11:15 PM
Old Wolf
Guest
 
Posts: n/a
Default Re: copy char*

Gary Wessle wrote:
Quote:
string a = "abcdef";
aa[80] = a.c_str();
This is an error: you can't assign to arrays
Quote:
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);

  #12  
Old November 15th, 2006, 11:25 PM
Gary Wessle
Guest
 
Posts: n/a
Default Re: copy char*

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?
  #13  
Old November 15th, 2006, 11:45 PM
BobR
Guest
 
Posts: n/a
Default Re: copy char*


Default User wrote in message <4s1h4hFtbh0qU1@mid.individual.net>...
Quote:
>BobR wrote:
>
<snip **incomplete** func1>
Quote:
Quote:
>// out: frst=abc last=def
>
>Did you actually try this?
Yes.
Quote:
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


  #14  
Old November 15th, 2006, 11:55 PM
BobR
Guest
 
Posts: n/a
Default Re: copy char*


Default User wrote in message <4s1h4hFtbh0qU1@mid.individual.net>...
Quote:
>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


  #15  
Old November 16th, 2006, 12:25 AM
Gary Wessle
Guest
 
Posts: n/a
Default Re: copy char*

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;
}
  #16  
Old November 16th, 2006, 12:25 AM
BobR
Guest
 
Posts: n/a
Default Re: copy char*


Gary Wessle wrote in message ...
Quote:
>
>#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';
Quote:
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.
Quote:
>}
>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.
Quote:
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



  #17  
Old November 16th, 2006, 02:05 AM
BobR
Guest
 
Posts: n/a
Default Re: copy char*


Gary Wessle wrote in message ...
Quote:
>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;
Quote:
strncpy(_second, pair+4, 3);
_second[ 4 ] = 0;
Quote:
>}
Quote:
>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;
}
Quote:
>const char* myType::second()const {
char* r;
Same here.
Quote:
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


  #18  
Old November 16th, 2006, 02:05 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: copy char*

"BobR" <RemoveBadBobR@worldnet.att.netwrote in message
news:m0P6h.279772$QZ1.61411@bgtnsc04-news.ops.worldnet.att.net...
Quote:
>
Default User wrote in message <4s1h4hFtbh0qU1@mid.individual.net>...
Quote:
>>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.


  #19  
Old November 16th, 2006, 03:25 AM
BobR
Guest
 
Posts: n/a
Default Re: copy char*


Jim Langston wrote in message ...
Quote:
>"BobR" wrote in message...
Quote:
>>
>Default User wrote in message ...
Quote:
>>>
>>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!!
Quote:
I suggested he use std::string anyway.
Must be some 'learning' thing, std::string would be too easy.

--
Bob R
POVrookie


  #20  
Old November 16th, 2006, 06:05 PM
Default User
Guest
 
Posts: n/a
Default Re: copy char*

Old Wolf wrote:
Quote:
Gary Wessle wrote:
Quote:
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
  #21  
Old November 16th, 2006, 06:15 PM
Default User
Guest
 
Posts: n/a
Default Re: copy char*

BobR wrote:
Quote:
>
Default User wrote in message <4s1h4hFtbh0qU1@mid.individual.net>...
Quote:
BobR wrote:
<snip incomplete func1>
Quote:
Quote:
// out: frst=abc last=def
Did you actually try this?
>
Yes.
>
Quote:
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.