• 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?
 
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
 
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
The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

 
ozoCommented:
As I said.
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.