My question is, what does this code do (from http://www.joelonsoftware.com/articles/CollegeAdvice.html):
while (*s++ = *t++);
the website says that the code above copies a string but I don't understand why...
does it have to do with pointers?
My question is, what does this code do (from http://www.joelonsoftware.com/articles/CollegeAdvice.html):
while (*s++ = *t++);
the website says that the code above copies a string but I don't understand why...
does it have to do with pointers?
Yes, it does have to do with pointers.
Edit: ...
The way to read the code is this: "the value that is pointed to by the pointer "s" (which gets incremented after this operation) gets the value which is pointed to by the pointer "t" (which gets incremented after this operation; the entire value of this operation evaluates to the value of the character copied; iterate across this operation until that value equals zero". Since the value of the string null terminator is the character value of zero ('/0'), the loop will iterate until a string is copied from the location pointed to by t to the location pointed to by s.
it copies a string because arrays are always passed by reference, and string is just a char array. Basically what is happening is (if i remember the term correctly) pointer arithmetic. Here's a bit more information from wikipedia on c arrays.
You are storing the value that was dereferenced from t in s and then moving to the next index via the ++.
It works by copying characters from the string pointed to by 't
' into the string pointed to by 's
'. For each character copies, both pointers are incremented. The loop terminates when it finds a NUL
character (equal to zero, hence the exit).
It is equivalent to this:
while (*t) {
*s = *t;
s++;
t++;
}
*s = *t;
When the char that t
points to is '\0'
, the while loop will terminate. Until then, it will copy the char that t
is pointing to to the char that s
is pointing to, then increment s
and t
to point to the next char in their arrays.
Yes this uses pointers, and also does all the work while evaluating the while condition. C allows conditional expressions to have side-effects.
The "*" operator derefereces pointers s and t.
The increment operator ("++") increments pointers s and t after the assignment.
The loop terminates on condition of a null character, which evaluates as false in C.
One additional comment.... this is not safe code, as it does nothing to ensure s has enough memory allocated.
HINTS:
ADVICE:
Say you have something like this:
char *someString = "Hello, World!";
someString points to the first character in the string - in this case 'H'.
Now, if you increment the pointer by one:
someString++
someString will now point to 'e'.
while ( *someString++ );
will loop until whatever someString points at becomes NULL, which is what signals the end of a string ("NULL Terminated").
And the code:
while (*s++ = *t++);
is equal to:
while ( *t != NULL ) { // While whatever t points to isn't NULL
*s = *t; // copy whatever t points to into s
s++;
t++;
}
This has so much going on under the covers:
while (*s++ = *t++);
The s
and t
variables are pointers (almost certainly characters), s
being the destination. The following steps illustrate what's happening:
*t
) are copied to s (*s
), one character.s
and t
are both incremented (++
).while
).while
continues until that character is zero (end of string in C
).Effectively, it's:
while (*t != 0) {
*s = *t;
s++;
t++;
}
*s = *t;
s++;
t++;
but written out in a much more compact way.
Its because the assignment operator (=) copies whats on the right to the left so
x=5;
makes x equal to five,
y=x;
makes y equal to x, and so forth. I assume what the while loop should do is
while(*s++==*t++)
the double equal signs (==, I'm not sure what the actual name is) returns true if both sides are equal, and false if they are not.
the assignment operator (=) does not technically return anything (it copies whats on the right side to the left), but if you actually wrote
while(*s++=*t++)
it would be the equivilent of writing
while(true)
Hopefully that helps you.
The aspect that is mysterious about this is the order of operations. If you look up the C language spec, it states that in this context, the order of operations is as follows:
1. * operator
2. = (assignment) operator
3. ++ operator
So the while loop then becomes, in english:
while (some condition): Take what is at address "t" and copy it over to location at address "s". Increment "s" by one address location. Increment "t" by one address location.
Now, what is "some condition"? Well ... the C lang spec also says that the value of an assignment expression is the assigned value itself, which in this case is "*t".
So "some condition" is "'t' points to something that is non-zero", or in a simpler way, "while the data at location 't' is not NULL".
starts a while loop....
*s = *t goes first, this assigns to what t points at to what s points at. ie, it copies a character from t string to s string.
what is being assigned is passed to the while condition... any non zero is "true" so it will continue, and 0 is false, it will stop.... and it just happens the end of a string is also zero.
s++ and t++ they increment the pointers
and it all starts again
so it keeps assigning looping, moving the pointers, until it hits a 0, which is the end of the string
Let's assume s
and t
are char *
s that point to strings (and assume s
is at least as large as t
). In C, strings all end in 0
(ASCII "NUL"), correct? So what does this do:
*s++ = *t++;
First, it does *s = *t
, copying the value at *t
to *s
. Then, it does s++
, so s
now points to the next character. And then it does t++
, so t
points to the next character. This has to do with operator precedence and prefix vs. postfix increment/decrement.
Operator precedence is the order in which operators are resolved. For a simple example, look:
4 + 2 * 3
Is this 4 + (2 * 3)
or (4 + 2) * 3
? Well, we know it is the first one because of precedence - the binary *
(multiplication operator) has higher precedence than the binary +
(addition operator), and is resolved first.
In *s++
, we have unary *
(pointer dereference operator) and unary ++
(postfix increment operator). In this case, ++
has higher precedence (also said to "bind tighter") than *
. If we had said ++*s
, we would increment the value at *s
rather than the address pointed to by s
because prefix increment has lower precedence* as dereference, but we used postfix increment, which has higher precedence. If we had wanted to use prefix increment, we could have done *(++s)
, since the parenthesis would have overridden all lower precedences and forced ++s
to come first, but this would have the undesirable side effect of leaving an empty character at the beginning of the string.
Note that just because it has higher precedence doesn't mean it happens first. Postfix increment specifically happens after the value has been used, which his why *s = *t
happens before s++
.
So now you understand *s++ = *t++
. But they put it in a loop:
while(*s++ = *t++);
This loop does nothing - the action is all in the condition. But check out that condition - it returns "false" if *s
is ever 0, which means *t
was 0, which means they were at the end of the string (yay for ASCII "NUL"). So this loop loops as long as there are characters in t
, and copies them dutifully into s
, incrementing s
and t
all the way. When this loop exits, s
has been NUL-terminated, and is a proper string. The only problem is, s
points to the end. Keep another pointer handy that points to the beginning of s
(i.e. s
before the while()
loop) - that will be your copied string:
char *s, *string = s;
while(*s++ = *t++);
printf("%s", string); // prints the string that was in *t
Alternatively, check this out:
size_t i = strlen(t);
while(*s++ = *t++);
s -= i + 1;
printf("%s\n", s); // prints the string that was in *t
We started by getting the length, so when we ended, we did more pointer arithmetic to put s
back at the beginning, where it started.
Of course, this code fragment (and all my code fragments) ignore buffer issues for simplicity. The better version is this:
size_t i = strlen(t);
char *c = malloc(i + 1);
while(*s++ = *t++);
s -= i + 1;
printf("%s\n", s); // prints the string that was in *t
free(c);
But you knew that already, or you'll soon ask a question on everyone's favorite website about it. ;)
* Actually, they have the same precedence, but that's resolved by different rules. They effectively have lower precedence in this situation.