• C

Describe order of operation while copying text

void strcpy(char *dst, char *src)
{
      while (*dst++ = *src++)
            ;
}

Open in new window


Please, Describe in which order operations are performed in this code.
Nusrat NuriyevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
dereference, then increment and assignment
0
historychefCommented:
ozo is not correct. The correct sequence is:

1. *src is dereferenced and the value in the location it references is fetched.
2. *dst is dereferenced, and the value fetched from Step 1 is stored into the location pointed to by dst.
3. Both src and dst are incremented (the C standard does not specify in which order these increments are done)
4. The value fetched in Step 1 (and stored in Step 2) is checked for being zero or nonzero.
5. If the value is nonzero, the next iteration of the while loop is done. Thanks to Step 3, both src and dst now point to the next-higher memory locations.
6. If the value is zero, the while loop terminates and execution continues with the statement following the empty statement ';'.
0
ozoCommented:
As I said.
0
How the Cloud Can Help You as an MSSP

Today, every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. Register today to learn more!

Nusrat NuriyevAuthor Commented:
guys, does this copying string algorithm works for all compilers? Some guys while interview me, tells me, if we have:
"Hello"
dst string after copy will be:
"Gfmmp"
0
ozoCommented:
Are you saying that src is "Hello"?
Where is *dst?
0
Nusrat NuriyevAuthor Commented:
Yes, "Hello" is string at the pointer src.
0
historychefCommented:
This example comes directly from Kernighan & Ritchie's book, so yes, it works on all compilers. It is not quite the strcpy() function from the C library, though, because that function returns the original address held in dst, so the library function looks something like this:

char* strcpy( char *dst, char *src )
{
        char *orig_dst = dst;

        while ( *dst++ = *src++ )
                ;

        return orig_dst;
}

Open in new window


The problem with ozo's answer is that the increments occur AFTER the assignment is done (they're post-increments, not pre-increments). If they were pre-increments (i.e., if the code were "while ( *++dst = *++src ) ..."), the copy operation would skip the first char value of both strings.

Note that the C standard specifically says that the results of strcpy() are undefined if the source and destination strings overlap.

Why don't you simply run the code and prove it to yourself that it works?
0
ozoCommented:
Were the some guys thinking of (*src++)++?
0
ozoCommented:
I did not say whether the increments occur after or before the assignment  is done.
I said  "increment and assignment", not  "increment, then assignment"
Nor does the C standard specify whether the increments occur after or before the assignment  is done.
There is no sequence point within "*dst++ = *src++"
I did say that the increment is done after the dereference, although technically it could be done before, as long as the dereference is done with the value before the increment.
Although the C standard says that the results of the strcpy in the standard library is undefined If copying takes place between objects that
overlap, it does not say that about the above code that happens to have the same name.
0
Nusrat NuriyevAuthor Commented:
So is there any compiler which acts different than others?
How do you determine sequence point?

this example is taken from Dennis Ritchie book.
0
ozoCommented:
Kernighan and Ritchie C was not exactly the same as ANSI/ISO C, but I think the sequence points were the same http://c-faq.com/expr/seqpoints.html
Which means that unless you do something undefined, like have dst=&src, or let them overrun the bounds of a valid object, conforming compilers should produce the same result.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.