I don't understand this for function.
This is a discussion on I don't understand this for function. within the C Programming forums, part of the General Programming Boards category; WHEN YOU TYPE
Code:
void func(char *s1, char *s2, char *s3){
while(*s1 != '
' ) s1++;
while(*s2 != '
-
I don't understand this for function.
WHEN YOU TYPE
Code:
void func(char *s1, char *s2, char *s3){
while(*s1 != '\0' ) s1++;
while(*s2 != '\0' ) s2++;
for(; *s1=*s2=*s3; s1++, s2++, s3++);
}
main() {
char s1[10] = "ABC", s2[10]="DEF";
char s3[10] = "GHI";
func(s1,s2,s3);
printf("%s, %s %s", s1, s2, s3);
} this code gives ABCGHI, DEFGHI GHI as the result....
how come there is a command assignment in the middle of for function
and why is the result like that? I would be very much appreciated if
you tell me step by step
Last edited by Dorky King; 06-12-2007 at 02:58 PM.
Reason: typed it wrong
-
come on experts!!! I'm desperate...
-
Frequently Quite Prolix
This
Code:
for(initialization; condition; increment) { /* ... */ } is the same as
Code:
initialization;
while(condition) {
/* ... */
increment;
} or
Code:
initialization;
value = condition;
while(value) {
/* ... */
increment;
value = condition;
} It follows that this
Code:
for(; *s1=*s2=*s3; s1++, s2++, s3++);
is the same as
Code:
while(*s1=*s2=*s3) {
s1++, s2++, s3++;
} or
Code:
value = *s1=*s2=*s3;
while(value) {
s1 ++;
s2 ++;
s3 ++;
value = *s1=*s2=*s3;
} Oh, and this
Code:
while(*s1 != '\0' ) s1++;
is the same as
Code:
value = (*s1 != '\0');
while(value) {
s1 ++;
value = (*s1 != '\0');
} Does that help answer your question?
[edit] Hint: loops like this
Code:
while(*s1 != '\0' ) s1++;
just increment s1 until s1 is pointing at the NULL terminator. Thus, after the first two lines of func(), s1 and s2 have both been incremented to point at their NULL terminators. [/edit]
dwk
Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards:
DaniWeb,
TPS
Unofficial Wiki FAQ:
cpwiki.sf.net
My website:
http://dwks.theprogrammingsite.com/
Projects:
codeform,
xuni,
atlantis,
nort,
etc.
-
Deathray Engineer
You post again after 4 minutes? Don't be lame. This isn't a drive-through fast food service, and even those might take longer than 4 minutes.
You posted a function that must be related to a programming course or something else designed to teach you, because it would be overly difficult to read otherwise.
As with the answer, I compiled it myself and got this:
Inside func, basically s1 and s2 will point to the end of the strings they respectively point to.
When the for loop occurs, this is what you have:
Code:
s1
|
|
v
A, B, C, \0
s2
|
|
v
D, E, F, \0
s3
|
|
v
G, H, I, \0 From there on, the condition of the for loop performs a copy. This is dangerous to do. In this program, it's guarenteed to work (I think), but the function assumes it's being passed large enough buffers, which can result in problems (similar to gets()).
-
opps.... the result is ABCGHI, DEFGHI GHI... I'm sorry mistake
-
>this code gives ABCGHI, DEFGHI DEF as the result....
I suggest a new compiler.
-
you guys are awesome... thank you so much!!!!
-
CSharpener
Very strange, but my output is
"ABCGHI, DEFGHI GHI"
I think - you miswrited it...
also main should be
int main(void)
If I have eight hours for cutting wood, I spend six sharpening my axe.
-
Now I've got another problem...
at the end of for statement, pointer s1, s2, s3are all pointing to the '\0' character of each
string.
when you come back to main and print each string why does it print the whole thing? is it supposed to???
and oh, another one.
When does the for statement officially end??
how does it get "false" value to end the statement???
-
and the hat of mystery
There's no answer to that level of impatience.
Oh wait, there is.
http://catb.org/~esr/faqs/smart-questions.html#urgent
-
Deathray Engineer
I answered these new questions in the other topic....
-
>When does the for statement officially end??
At the semicolon.
>how does it get "false" value to end the statement???
A false is a zero, so when the value of *s3 becomes zero (the string terminator is found), then *s2 will be assigned to 0, as will *s1, so the expression evaluates to 0, or false.
I'm not sure I can do a good job of explaining question 1, so I'll defer.
-
CSharpener
for ends when condition is false
This *s1=*s2=*s3 is false when the return value is 0, the return value is the value assigned to *s1. So when the null-character from the string s3 is copied to s2 ans s1 - for loop ends
As to the first questing - parameters in C are passed by value, so original pointers are not changed, func function changes only the local copies of the pointers s1,s2,s3
If I have eight hours for cutting wood, I spend six sharpening my axe.
Popular pages Recent additions
Similar Threads
-
By Matt13 in forum C Programming
Replies: 2
Last Post: 06-21-2004, 08:37 AM