The characters are 'g', 'o', 'o','d', 'e', 'v', 'e', 'n', 'i' 'n', 'g', '\0'. That last one is simply a character whose value is zero, and is used to endicate the end of a C- style . You know that there is no ? That is, if you try to write anything in that space, the results are indeterminate. It is OK for user programs to read from this space, but writing is not allowed (at least it is not legal). The first time through the loop, the instruction is to take the character pointed to by s and store it into the memory location pointed to by v (and subequently increment the values of s and v). Here's the problem: you are not allowed to store anything into the memory location pointed to by v. If you want to copy the characters of the original string somewhere else, it has to be to some place that you are allowed to write. So, you could change the declaration for v to something likechar v. However, now the following is wrong: while(*v++ = *s++). Why is it wrong? Well, v is now the name of an array. You can't change v in any way, so v++ is illegal. However, consider the statement that was already in the program. Now if v is the name of an array, then when you use the name by itself (no . So, the pointer p points to the first character in the array. Then you can change the while statement to: while(*p++ = *s++). The value of the expression inside the parentheses is equal to the value of the assignment statement. Some people think it is clearer to write it like the following: while((*p++ = *s++) != 0). But the results are the same, and C programmers sometimes like to confuse people by using little shortcuts like the way it was written by whoever created the code in your original post. Now, each character is copied from the original string into successive locations within the array v. The copying stops when the terminating zero is copied (since at that time the value of the expression is equal to zero). Now, if you want to print out the.
C Program to Copy all elements of an array into Another array. C Program to Read integers into an array and Reversing them using Pointers; C Program to Implement Stack Operations Using. Copying characters from one string to another w/ pointers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |