| | |
One line of code copy char array using pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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

ie
C++ Syntax (Toggle Plain Text)
while( *(outstr++) = *(instr++) )
>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').
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.
>>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.
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.
>>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) 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)."
>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)
while( *outstr++ = *instr++);
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.
I'm here to prove you wrong.
•
•
•
•
>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').
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"
by Robert Frost the "The Road Not Taken"
![]() |
Similar Threads
- Safe Array (C++)
- Pointers (C++)
- adding data into an char array (C++)
- Pointers (Part II) (C)
Other Threads in the C++ Forum
- Previous Thread: how to pass string into function
- Next Thread: Create an array of 100 random integers in a sequential search
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






