944,126 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 28293
  • C++ RSS
Apr 27th, 2006
0

One line of code copy char array using pointers

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nquaoser is offline Offline
2 posts
since Apr 2006
Apr 27th, 2006
0

Re: One line of code copy char array using pointers

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 27th, 2006
0

Re: One line of code copy char array using pointers

I actually just figured it out from some other website. It's very simple: while( *outstr++ = *instr++); Thanks very much though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nquaoser is offline Offline
2 posts
since Apr 2006
Apr 27th, 2006
0

Re: One line of code copy char array using pointers

I wonder if people actually figure things out by themselves anymore. It seems to me that the web stifles creativity to a certain extent.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 28th, 2006
0

Re: One line of code copy char array using pointers

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
C++ Syntax (Toggle Plain Text)
  1. while( *(outstr++) = *(instr++) )
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Apr 28th, 2006
0

Re: One line of code copy char array using pointers

>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').
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 8th, 2007
0

Re: One line of code copy char array using pointers

while( *(outstr++) = *(instr++) );

that ; almost killed me... why do we need that
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chikkubhai is offline Offline
4 posts
since Oct 2007
Oct 8th, 2007
0

Re: One line of code copy char array using pointers

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Oct 9th, 2007
1

Re: One line of code copy char array using pointers

>>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:
C++ Syntax (Toggle Plain Text)
  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)."
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 9th, 2007
0

Re: One line of code copy char array using pointers

Click to Expand / Collapse  Quote originally posted by Narue ...
>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...
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to pass string into function
Next Thread in C++ Forum Timeline: Create an array of 100 random integers in a sequential search





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC