Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

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.
Avatar of ozo
ozo
Flag of United States of America image

dereference, then increment and assignment
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 ';'.
As I said.
Avatar of Nusrat Nuriyev

ASKER

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"
Are you saying that src is "Hello"?
Where is *dst?
Yes, "Hello" is string at the pointer src.
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?
Were the some guys thinking of (*src++)++?
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.
So is there any compiler which acts different than others?
How do you determine sequence point?

this example is taken from Dennis Ritchie book.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial