One line of code copy char array using pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 2
Reputation: nquaoser is an unknown quantity at this point 
Solved Threads: 0
nquaoser nquaoser is offline Offline
Newbie Poster

One line of code copy char array using pointers

 
0
  #1
Apr 27th, 2006
Help! I need to copy a char array initialized as a string from one array to another, using a function containing one line of code. Here is the code:

#include <iostream>
using namespace std;
void cpystr(char *instr, char *outstr);
void cpystr(char *instr, char *outstr)
{
//Insert line of code here
}
int main()
{
char in[]={'d', 'f', 'g', '\0'};
char out[4];
 
cpystr(in, out);
cout << "in: " << in << endl;
cout << "out: " << out << endl;
 
return 0;
}
 
Thanks!
Last edited by cscgal; Apr 28th, 2006 at 9:57 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: One line of code copy char array using pointers

 
0
  #2
Apr 27th, 2006
What have you tried? The answer you're looking for is a classic example of a controversial solution, but it's not overly difficult if you know how loops and pointers work.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 2
Reputation: nquaoser is an unknown quantity at this point 
Solved Threads: 0
nquaoser nquaoser is offline Offline
Newbie Poster

Re: One line of code copy char array using pointers

 
0
  #3
Apr 27th, 2006
I actually just figured it out from some other website. It's very simple: while( *outstr++ = *instr++); Thanks very much though!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: One line of code copy char array using pointers

 
0
  #4
Apr 27th, 2006
I wonder if people actually figure things out by themselves anymore. It seems to me that the web stifles creativity to a certain extent.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: One line of code copy char array using pointers

 
0
  #5
Apr 28th, 2006
I can see why that is a controversial solution. it would be very easy to mis-read the intention of that code. The least you could do would be to provide brackets around the identifier and post-increment
ie
  1. while( *(outstr++) = *(instr++) )
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: One line of code copy char array using pointers

 
0
  #6
Apr 28th, 2006
>it would be very easy to mis-read the intention of that code
That's one reason. Here are a few more:

1) It's too compact to be readable.
2) The performance is deceptive (it's usually slower).
3) The tricks used are obscure.
4) Lint goes nuts (abuse of dangerous constructs).
5) The mechanics are too subtle (notably, dealing with '\0').
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: chikkubhai is an unknown quantity at this point 
Solved Threads: 0
chikkubhai chikkubhai is offline Offline
Newbie Poster

Re: One line of code copy char array using pointers

 
0
  #7
Oct 8th, 2007
while( *(outstr++) = *(instr++) );

that ; almost killed me... why do we need that
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: One line of code copy char array using pointers

 
0
  #8
Oct 8th, 2007
>>why do we need that
To complete the while statement. Normally putting a semicolon there would be a bug, but not in this case because there is nothing else to do.

>>It's too compact to be readable.
Then the reader doesn't know C language very well, probably a newbe. Looks perfectly ok to me.

>>The performance is deceptive (it's usually slower).
slower than what? possibly strcpy() is faster, depending on how the compiler optimized it.

>>The tricks used are obscure.
What tricks? And how are they obscure.

>>The mechanics are too subtle (notably, dealing with '\0').
Not subtle at all if you realize what that loop is doing.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: One line of code copy char array using pointers

 
1
  #9
Oct 9th, 2007
>>It's too compact to be readable.
>Then the reader doesn't know C language very well, probably a newbe. Looks perfectly ok to me.

>>The tricks used are obscure.
>What tricks? And how are they obscure.

>>The mechanics are too subtle (notably, dealing with '\0').
>Not subtle at all if you realize what that loop is doing.
Okay, let's break it down by difficult concepts. Here's the original code, to save scrolling:
  1. while( *outstr++ = *instr++);
1) You have to know the precedence/associativity of * and ++. This is a notoriously good area to screw up, even for experienced C programmers (to the point where ++ is often banned in complex expressions).

2) You have to know the implicit test made by a loop, and that it correctly checks for the end of a string. Experienced programmers usually have to think about this to get it right. I'm one of them, which is why my style calls for explicit tests except for exact boolean comparisons.

3) You have to know that because the condition is also an assignment, the null character is correctly added to the string without further logic.

4) You have to parse the entire line before realizing that the null body is intended rather than a grievous error.

That's a lot of prerequisite (not to mention intimate) knowledge for understanding such a simple operation.

>slower than what?
Other solutions that vary depending on the compiler and optimization settings. My wording was poor though, I should have said "The performance is deceptive (it could easily be slower)."
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: One line of code copy char array using pointers

 
0
  #10
Oct 9th, 2007
Originally Posted by Narue View Post
>it would be very easy to mis-read the intention of that code
That's one reason. Here are a few more:

1) It's too compact to be readable.
2) The performance is deceptive (it's usually slower).
3) The tricks used are obscure.
4) Lint goes nuts (abuse of dangerous constructs).
5) The mechanics are too subtle (notably, dealing with '\0').
for anyone who doesn't know what lint is{i didn't!} read this.

PS:: although i am not an experienced programmer, for purely aesthetically reasons {plus all the other reasons narue wrote} i never liked this construct...
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC