Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

for (; *from !='\0'; ++from, ++to) ???

+1 vote
asked Jan 29, 2015 in CS50 by JohnHilton (1,570 points)

While working through "Programming in C" in chapter 11, example 11.13  the for statement starts with a semicolon inside of the brackets.

void copystring (char *to, char *from)
    {
    for (; *from !='\0'; ++from, ++to)
    *to = *from;
    *to = '\0';
    }

As I do not understand why the semicolon is used here, can anyone offer me an explanation.

I should add that it compiles and runs as expected.

Thanks.

1 Answer

0 votes
answered Jan 29, 2015 by Faïza Harbi (11,960 points)
a for is supposed to have 3 phases: initialization aka start, condition aka stop, and update aka step.

Here nothing before the semi-colon is showing that the initialisation/start part is empty.

It's usually when we don't actually need something to start, to enter the for loop.
commented Jan 29, 2015 by JohnHilton (1,570 points)
Thanks, I had forgotten that one.
Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

190 questions

248 answers

217 comments

3,602 users

2 Online
0 Member And 2 Guest
...